python中特殊的方法和屬性

2021-10-19 01:34:06 字數 716 閱讀 1684

class chu(object):

def __init__(self, name,age):

self.name = name

self.age = age

# print("楚國")

def local(self):

print("楚國在湖北和湖南區域")

class chen(chu):

def __init__(self,name,age,wenzi):

super().__init__(name,age)

self.wenzi = wenzi

# print("陳國")

# 重寫local方法

def local(self,loc):

# 呼叫父類中的方法

super().local()

print("陳的國都",loc)

c = chen("陳",30,"漢字")

c.local("漢城")

# 檢視例項物件的屬性字典

print(c.__dict__)

# 檢視類物件的屬性字典

print(chu.__dict__)

# 檢視子類繼承父類的屬性

python中屬性訪問的特殊方法

python中屬性訪問的特殊方法 折射 class a def init self self.a 10 self.b 100 a a 1.1getattr obj,屬性名 獲得例項對應的屬性的值 print getattr a,a 10 1.2setattr obj,屬性名 值 修改例項對應的屬性的...

python中重寫和特殊構造方法

class bird def init self self.hungry true defeat self if self.hungry print ahaaaaa.self.hungry false else print no,thanks class somebird bird def init...

Python (類)例項方法的特殊屬性

自定義函式的特殊屬性已經專門整理過一篇 python 自定義函式的特殊屬性 方法的特殊屬性與其稍有不同,我們通過下面這個例子展開介紹 class a def foo self a method print hellow world bar foo classmethod def clsmtd cls...