Here in this lesson i can’t understand why we have to use “self” argument and what does it refer to.
What does actually this mean ?
How can self argument have attribute here ?
Here why i have to use self for example ?
self is the current instance/object of a class, Python passes it implicitly when we call a method on a instance/object
class variable (like dog_time_dilation) are also implicitly bound to self.
Instance variable have to explicitly bound:
class Example: def __init__(self, hello): self.hello = hello def print_instance_variable(self): print(self.hello) example = Example('hello world') example.print_instance_variable()
we can also call a method on the class itself, then we do have to provide argument for self:
In this example, we explicitly “bind”
self parameter to
So that means, self actually refers to class. For example, for using objects in functions we are using self argument with attribute(object’s name) to call it in functions.
Dog is the class.
pip_pitbull is the instance/object. Which self refers the instance/object. Not the class
Classes have methods (like
area). I wouldn’t name them functions, that is confusing, this is a function:
def myFunc(): print("I am a function")