Skip to content

Commit dd9c5b3

Browse files
author
Vimal
committed
* Renamed 25-class-methods.py -> 27-classmethod-1.py
* Renamed 27-classmethod-1.py -> 28-classmethod-2.py * Updated 29-staticmethod-1.py * Renamed 30-magicmethods-1.py -> 31-magicmethods-1.py * Renamed 31-magicmethods-2.py -> 32-magicmethods-2.py * Renamed 32-attribute-encapsulation-1.py -> 33-attribute-encapsulation-1.py * Deleted static_methods.py
1 parent 1e73002 commit dd9c5b3

8 files changed

+57
-72
lines changed

25-class-methods.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

27-classmethod-1.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22

3-
# 27-classmethod-1.py
3+
# 19-class_methods-1.py
44

5-
# Classmethods are decorators which are inbuilt in Python.
6-
# We decorate a function as a classemethod using the decorator
5+
# A classmethod is an inbuilt decorator which is called on functions via
76
# @classmethod.
87

9-
# Class methods are used for functions which need not be
10-
# called via an instance. Certain use cases may be:
8+
# The @classmethod decorator marks the function/method as bound to the
9+
# class and not to an instance.
1110

12-
# a) Creating instances take resources, hence the methods/functions
13-
# which need necessarily
11+
# Remember that we used 'self' in a function within a class, which denoted
12+
# the instance. In class methods, we use `cls` which denotes the class
13+
# rather than the instance.
14+
15+
# The following example is a very simple explanation of class-methods.
16+
17+
18+
class MyClass(object):
19+
@classmethod
20+
def class_1(cls):
21+
print("Class method 1")
22+
23+
def class_2(self):
24+
print("Self/Instance method 1")
25+
26+
27+
print("Calling the class `MyClass` directly without an instance:")
28+
MyClass.class_1()
29+
# MyClass.class_2()
30+
31+
# NOTE: You will want to comment `MyClass.class_2()` once you hit the `TypeError`
32+
# to continue with the examples below.
33+
34+
print("\nCalling the instance `MyClass()`:")
35+
MyClass().class_1()
36+
MyClass().class_2()

28-classmethod-2.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
3+
# 27-classmethod-1.py
4+
5+
# Classmethods are decorators which are inbuilt in Python.
6+
# We decorate a function as a classemethod using the decorator
7+
# @classmethod.
8+
9+
# Class methods are used for functions which need not be
10+
# called via an instance. Certain use cases may be:
11+
12+
# a) Creating instances take resources, hence the methods/functions
13+
# which need necessarily

29-staticmethod-1.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
#!/usr/bin/env python3
22

3+
# 29-staticmethod-1.py
4+
5+
# Refer
6+
# https://arvimal.wordpress.com/2016/06/12/instance-class-static-methods-object-oriented-programming/
7+
8+
# Static methods are functions/methods which doesn’t need a binding to a class or an instance.
9+
# Static methods, as well as Class methods, don’t require an instance to be called.
10+
# Static methods doesn’t need self or cls as the first argument since it’s not bound to an instance or class.
11+
# Static methods are normal functions, but within a class.
12+
# Static methods are defined with the keyword @staticmethod above the function/method.
13+
# Static methods are usually used to work on Class Attributes.
14+
315

416
class InstanceCounter(object):
517
count = 0
@@ -24,4 +36,4 @@ def filterint(value):
2436
print(a.val)
2537
print(b.val)
2638
print(c.val)
27-
print(a.filterint(100))
39+
print(a.filterint(100))
File renamed without changes.
File renamed without changes.
File renamed without changes.

static_methods.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)