Skip to content

Commit e6567fc

Browse files
author
Vimal
committed
* Updated 27-classmethod-1.py
* Updated 28-classmethod-2.py * Updated 29-staticmethod-1.py
1 parent 7e881dd commit e6567fc

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

27-classmethod-1.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414

1515
# The following example is a very simple explanation of class-methods.
1616

17+
# class_1() is a class method while class_2() is an instance method.
18+
19+
# Class methods can be accessed by the class as well as the instance.
20+
21+
# Instance methods can only be accessed by the Instance. That's why in this example, MyClass.class_2() will fail with an error.
22+
23+
# NOTE : For the class MyClass:
24+
# MyClass is the class itself
25+
# MyClass() is an instance
26+
1727

1828
class MyClass(object):
1929
@classmethod

28-classmethod-2.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# 28-classmethod-2.py
44

5+
# Reference: https://jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
6+
57
# Classmethods are decorators which are inbuilt in Python.
68
# We decorate a function as a classemethod using the decorator
79
# @classmethod.
@@ -13,12 +15,12 @@
1315
# which need necessarily
1416

1517

16-
class InstanceCounter(object):
18+
class MyClass(object):
1719
count = 0
1820

1921
def __init__(self, val):
2022
self.val = val
21-
InstanceCounter.count += 1
23+
MyClass.count += 1
2224

2325
def set_val(self, newval):
2426
self.val = newval
@@ -30,14 +32,14 @@ def get_val(self):
3032
def get_count(cls):
3133
return cls.count
3234

33-
a = InstanceCounter(10)
34-
print("\nValue of object : %s" % a.get_val())
35-
print(InstanceCounter.get_count())
35+
object_1 = MyClass(10)
36+
print("\nValue of object : %s" % object_1.get_val())
37+
print(MyClass.get_count())
3638

37-
b = InstanceCounter(20)
38-
print("\nValue of object : %s" % b.get_val())
39-
print(InstanceCounter.get_count())
39+
object_2 = MyClass(20)
40+
print("\nValue of object : %s" % object_2.get_val())
41+
print(MyClass.get_count())
4042

41-
c = InstanceCounter(40)
42-
print("\nValue of object : %s" % c.get_val())
43-
print(InstanceCounter.get_count())
43+
object_3 = MyClass(40)
44+
print("\nValue of object : %s" % object_3.get_val())
45+
print(MyClass.get_count())

29-staticmethod-1.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22

33
# 29-staticmethod-1.py
44

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-
15-
16-
class InstanceCounter(object):
5+
"""
6+
# Refer https://arvimal.wordpress.com/2016/06/12/instance-class-static-methods-object-oriented-programming/
7+
# a) Static methods are functions methods which doesn’t need a binding
8+
# to a class or an instance.
9+
# b) Static methods, as well as Class methods, don’t require an instance
10+
# to be called.
11+
# c) Static methods doesn’t need self or cls as the first argument
12+
# since it’s not bound to an instance or class.
13+
# d) Static methods are normal functions, but within a class.
14+
# e) Static methods are defined with the keyword @staticmethod
15+
# above the function/method.
16+
# f) Static methods are usually used to work on Class Attributes.
17+
"""
18+
19+
20+
class MyClass(object):
1721
count = 0
1822

1923
def __init__(self, val):
2024
self.val = self.filterint(val)
21-
InstanceCounter.count += 1
25+
MyClass.count += 1
2226

2327
@staticmethod
2428
def filterint(value):
@@ -29,9 +33,9 @@ def filterint(value):
2933
return value
3034

3135

32-
a = InstanceCounter(5)
33-
b = InstanceCounter(10)
34-
c = InstanceCounter(15)
36+
a = MyClass(5)
37+
b = MyClass(10)
38+
c = MyClass(15)
3539

3640
print(a.val)
3741
print(b.val)

0 commit comments

Comments
 (0)