Powered | | ?? ???? ?? ?? ??? ???? ?? ??? ????.
type(obj): ?? ?? ??? ?????.
isinstance(obj, type): ??? ??? ?????. ?:
hasattr(obj, attr): ??? ??? ??/???? ??? ?????.
getattr(obj, attr[, default]) ??/???? ?? ?????. ?? ??? ??? ???? ?????(???? ???? ??? ??). ??? ??? AttributeError ??? ?????.
setattr(obj, attr, value): ??/???? ?? ?????. to obj.attr=value;
dir(obj): ? ?? ??? ?? ?? ? ??? ?? ??? ?????.
2. ??? ??? ??
??? ???? ???? ??? ?? ????. ??? ???? ??? ??? ??? ?????. ????? ??? ???? ????. ?? ?? ?????? ????? Python ????? ??? ???? ? ?? ????.
??? ??? ??? ???? ??? ??? ?????.
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
class User(object):
def upgrade(self):
pass
def _buy_equipment(self):
pass
def __pk(self):
pass
3. Method decorator
@classmethod? ?? ?? ??? ??? ???? ?? ?????.
@property? ??? ?? ???? ? ????. ?? ??? ??? ?????.
???? ?? ?? ?????:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
class UserInfo(object):
lv = 5
def __init__(self, name, age, account):
self.name = name
self._age = age
self.__account = account
def get_account(self):
return self.__account
@classmethod
def get_name(cls):
return cls.lv
@property
def get_age(self):
return self._age
if __name__ == '__main__':
userInfo = UserInfo('兩點(diǎn)水', 23, 347073565);
# 打印所有屬性
print(dir(userInfo))
# 打印構(gòu)造函數(shù)中的屬性
print(userInfo.__dict__)
# 直接使用類名類調(diào)用,而不是某個(gè)對(duì)象
print(UserInfo.lv)
# 像訪問屬性一樣調(diào)用方法(注意看get_age是沒有括號(hào)的)
print(userInfo.get_age)
?? ??:

?? ????? ????? ? ????. ?? ???? ???? ????. ???? ? ??? ?? ?? ??????~
? ??? ??? ???? ???? ????.