__dict__
class Robot:
population = 0
def __init__(self, name, code):
self.name = name
self.code = code
Robot.population += 1
def say_hi(self):
print(f"Hi!,my master call me {self.name}")
def cal_add(self, a, b):
return a + b
def die(self):
print(f"{self.name} is being destroyed")
Robot.population -= 1
if Robot.population == 0:
print(f"{self.name} was last one.")
else:
print(f"There are still {Robot.population} robots working.")
@classmethod
def how_many(cls):
print(f"We have {cls.population} robots")
siri = Robot("Siri", 1234123)
print(Robot.__dict__)
print("=====================")
print(siri.__dict__)
print("=====================")
siri.say_hi()
Robot.say_hi(siri)
siri.how_many()
print("=====================")
siri.population = 20
print(siri.population)
print(Robot.population)
{'__module__': '__main__', 'population': 1, '__init__': <function Robot.__init__ at 0x1028b71c0>, 'say_hi': <function Robot.say_hi at 0x1028b7250>, 'cal_add': <function Robot.cal_add at 0x1028b72e0>, 'die': <function Robot.die at 0x1028b7370>, 'how_many': <classmethod(<function Robot.how_many at 0x1028b7400>)>, '__dict__': <attribute '__dict__' of 'Robot' objects>, '__weakref__': <attribute '__weakref__' of 'Robot' objects>, '__doc__': None}
=====================
{'name': 'Siri', 'code': 1234123}
=====================
Hi!,my master call me Siri
Hi!,my master call me Siri
We have 1 robots
=====================
20
1
dir()
print(dir(siri))
print(dir(Robot))
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'cal_add', 'code', 'die', 'how_many', 'name', 'population', 'say_hi']
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'cal_add', 'die', 'how_many', 'population', 'say_hi']
__doc__
class Robot:
"""
author:codakcodak
decription:자신을 소개하고 더하고 끝낼 수 있는 기능을 담은 로봇객체입니다.
"""
population = 0
def __init__(self, name, code):
self.name = name
self.code = code
Robot.population += 1
def say_hi(self):
print(f"Hi!,my master call me {self.name}")
def cal_add(self, a, b):
return a + b
def die(self):
print(f"{self.name} is being destroyed")
Robot.population -= 1
if Robot.population == 0:
print(f"{self.name} was last one.")
else:
print(f"There are still {Robot.population} robots working.")
@classmethod
def how_many(cls):
print(f"We have {cls.population} robots")
print(Robot.__doc__)
author:codakcodak
decription:자신을 소개하고 더하고 끝낼 수 있는 기능을 담은 로봇객체입니다.
__class__
- 클래스 또는 인스턴스가 해당하는 클래스 정보를 출력
print(Robot)
print(siri.__class__)
<class '__main__.Robot'>
<class '__main__.Robot'>