Python Instance Methods
Instance methods are functions defined in a class that can refer to values stored in instance variables, as well as having access to class variables.
Python
Copy Code
class MyClass: # The class constructor def __init__(self): self.variable = 42 def myMethod(self): return self.variable object = MyClass() object.myMethod() # myMethod will have "object" passed to it as the "self" variable
Here we define a class that has an instance variable myMethod
. All instance methods
have self
automatically passed in as the
first parameter, meaning that method has access to the object that called it.