You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The first method on MyClass, called method, is a regular instance method.
16
+
the method takes one parameter, self, which points to an instance of MyClass when the method is called
17
+
(but of course instance methods can accept more than just one parameter).
18
+
19
+
It can modify object's state and class state.
20
+
Through the self parameter, instance methods can freely access attributes and other methods on the same object.
21
+
Not only can they modify object state, instance methods can also access the class itself through the self.__class__ attribute. This means instance methods can also modify class state.
22
+
'''
23
+
'''
24
+
>>> obj = MyClass()
25
+
>>> obj.method()
26
+
('instance method called', <MyClass instance at 0x10205d190>)
27
+
This confirmed that method (the instance method) has access to the object instance (printed as <MyClass instance>) via the self argument.
28
+
29
+
When the method is called, Python replaces the self argument with the instance object, obj.
30
+
31
+
we can also do like this
32
+
>>> MyClass.method(obj)
33
+
('instance method called', <MyClass instance at 0x10205d190>)
34
+
'''
35
+
36
+
'''
37
+
Class Methods-
38
+
@classmethod decorator to flag it as a class method.
39
+
40
+
Instead of accepting a self parameter, class methods take a cls parameter that points to the class—and not the object instance—when the method is called.
41
+
Because the class method only has access to this cls argument, it can’t modify object instance state.
42
+
That would require access to self.
43
+
However, class methods can still modify class state that applies across all instances of the class.
44
+
'''
45
+
'''
46
+
>>> obj.classmethod()
47
+
('class method called', <class MyClass at 0x101a2f4c8>)
48
+
Calling classmethod() showed us it doesn’t have access to the <MyClass instance> object, but only to the <class MyClass> object, representing the class itself (everything in Python is an object, even classes themselves).
49
+
'''
50
+
51
+
'''
52
+
Static Methods-
53
+
@staticmethod decorator to flag it as a static method.
54
+
55
+
This type of method takes neither a self nor a cls parameter (but of course it’s free to accept an arbitrary number of other parameters).
56
+
57
+
Therefore a static method can neither modify object state nor class state.
58
+
Static methods are restricted in what data they can access - and they’re primarily a way to namespace your methods.
- Inside classes, you can define functions or methods that are part of this class
39
+
- Difference between functions and methods
40
+
* Functions can be called only by its name, as it is defined independently. But methods can't be called by its name only, we need to invoke the class by a reference of that class in which it is defined, i.e. method is defined within a class and hence they are dependent on that class.
38
41
39
42
```
40
43
def method1 (self):
@@ -57,6 +60,11 @@ class myClass():
57
60
- “self”
58
61
- The self-argument refers to the object itself. Hence the use of the word self. So inside this method, self will refer to the specific instance of this object that’s being operated on.
59
62
- Self is the name preferred by convention by Pythons to indicate the first parameter of instance methods in Python. It is part of the Python syntax to access members of objects
63
+
64
+
- Types of methods- [Check code for reference](Methods/methods.py))
65
+
* Instance Method
66
+
* Class Method
67
+
* Static Method
60
68
------------
61
69
------------
62
70
#### 03. Objects
@@ -107,4 +115,9 @@ The __init__ method lets the class initialize the object's attributes and serves
0 commit comments