Skip to content

Commit 2a7a7b5

Browse files
Methods
1 parent 529834e commit 2a7a7b5

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

Diff for: Methods/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# ignore all logs
2+
*.log
3+
# ignore cache
4+
__pycache__
5+
*.pyc

Diff for: Methods/methods.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class MyClass:
2+
def method(self):
3+
return 'instance method called', self
4+
5+
@classmethod
6+
def classmethod(cls):
7+
return 'class method called', cls
8+
9+
@staticmethod
10+
def staticmethod():
11+
return 'static method called'
12+
13+
'''
14+
Instance Methods-
15+
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.
59+
'''
60+
'''
61+
>>> obj.staticmethod()
62+
'static method called'
63+
'''

Diff for: README.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
00. [OOPs](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#what-do-you-understand-by-oops)
44
01. [Classes](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#01-classes)
5-
02. [Methods/Functions](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#02-methodsfunctions)
5+
02. [Methods-Functions](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#02-methodsfunctions)
66
03. [Objects](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#03-objects)
77
04. [Constructors](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#04-constructors)
8+
05. [Inheritance](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#04-constructors)
89

910
------------
1011
## What do you understand by OOPs?
@@ -32,9 +33,11 @@ class myClass():
3233
------------
3334
------------
3435

35-
#### 02. Methods/Functions
36+
#### 02. Methods-Functions
3637

3738
- 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.
3841

3942
```
4043
def method1 (self):
@@ -57,6 +60,11 @@ class myClass():
5760
- “self”
5861
- 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.
5962
- 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
6068
------------
6169
------------
6270
#### 03. Objects
@@ -107,4 +115,9 @@ The __init__ method lets the class initialize the object's attributes and serves
107115
def __init__(self):
108116
# body of the constructor
109117
```
118+
------------
119+
------------
120+
#### 05. Inheritance
121+
122+
110123
------------

0 commit comments

Comments
 (0)