# Class Methods
class InsCounter():
count = 0
def __init__(self, val):
self.val = val
InsCounter.count += 1
def getValue(self):
return self.val
@classmethod
def getCount(cls):
return cls.count
a = InsCounter(1)
b = InsCounter(2)
c = InsCounter(3)
for o in [a, b, c]:
print("val of obj: {}".format(o.getValue()))
print("count: {}".format(o.getCount()))
# 결과
val of obj: 1
count: 3
val of obj: 2
count: 3
val of obj: 3
count: 3
# Static Methods
class InsCounter():
count = 0
def __init__(self, val):
self.val = self.filterint(val)
InsCounter.count += 1
@staticmethod
def filterint(val):
if not isinstance(val, int):
return 0
else:
return val
a = InsCounter(1)
b = InsCounter(2)
c = InsCounter(3)
print(a.val)
print(b.val)
print(c.val)
# 결과
1
2
3
댓글 영역