python 類 例項 繼承中變數的id是否變化

2021-08-31 09:28:59 字數 1178 閱讀 9014

'''

注: 估計這麼亂的**只有我自己能看。通過這段**的驗證,證明了在類和例項的相對關係中,

把變數儲存為字串,數字,在各個類、例項之間變換的時候,變數的id是變化的,

把變數儲存為列表和字典的時候,id位址是不變的。id位址變與不變用於確認是否是同乙份資料,

看一下這個變數是否是全域性變數

'''class initparam(object):

def __init__(self):

self.name={}

print(id(self.name))

def set_name(self,name,score):

self.name[name]=score

def get_name_id(self):

print(id(self.name))

params=initparam()

params.name

params.get_name_id()

class pyquantbook():

def __init__(self,params):

self.name=params.name

print(id(self.name))

def set_name(self,name,score):

self.name[name]=score

def get_name_id(self):

print(id(self.name))

book=pyquantbook(params)

book.set_name('guowenjie',60)

book.name

book.get_name_id()

class a():

def __init__(self,book):

self.name=book.name

def set_name(self,name,score):

self.name[name]=score

def get_name_id(self):

print(id(self.name))

a=a(book)

print(a.name)

print(id(a.name))

book.set_name('yunjinqi',60)

python中的類,類變數,例項變數

這幾天寫 的時候碰到乙個python中的小問題,找了好半天才解決,所以記錄下來。classa object def init self self.x y def call self for i in range 5 print x self.x print y y 輸出的結果是 x 5,5,5,5,...

python中類變數和例項變數 之例項變數

class test def f self,name self.name name def f1 self print self.name 例項變數為例項物件特有資料 x1 test 例項化test類,建立例項物件x1 x2 test 例項化test類,建立例項物件x2 設定例項屬性 方式1 x1....

python子類如何繼承父類的例項變數?

型別1 父類和子類的例項變數均不需要傳遞 classa object def init self self.name cui defget name self return self.name class b a def init self super b,self init self.age 12...