forked from arvimal/oop_with_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
30-staticmethod-2.py
38 lines (28 loc) · 1.15 KB
/
30-staticmethod-2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
from __future__ import print_function
# 30-staticmethod-2.py
# Refer
# https://arvimal.wordpress.com/2016/06/12/instance-class-static-methods-object-oriented-programming/
# Static methods are functions/methods which doesn’t need a binding to a class or an instance.
# Static methods, as well as Class methods, don’t require an instance to be called.
# Static methods doesn’t need self or cls as the first argument since it’s not bound to an instance or class.
# Static methods are normal functions, but within a class.
# Static methods are defined with the keyword @staticmethod above the function/method.
# Static methods are usually used to work on Class Attributes.
class MyClass(object):
# A class attribute
count = 0
def __init__(self, name):
print("An instance is created!")
self.name = name
MyClass.count += 1
# Our class method
@staticmethod
def status():
print("The total number of instances are ", MyClass.count)
print(MyClass.count)
my_func_1 = MyClass("MyClass 1")
my_func_2 = MyClass("MyClass 2")
my_func_3 = MyClass("MyClass 3")
MyClass.status()
print(MyClass.count)