obj['hello'] = 'world'
class
people
(object):
def__init__
(self):
self.name = "h"
def__getattribute__
(self, item):
print("get->",item)
return object.__getattribute__(self,item)
def__getattr__
(self, item):
print("hello")
defm(self):
self.name = 'a'
p = people()
p.m()
print(p.name)
# get-> m
# get-> name
# a
class
people
(object):
def__init__
(self):
self.name = "h"
def__setattr__
(self, key, value):
super(people, self).__setattr__(key,value)
#self.__dict__[key] = value
print("hello")
defm(self):
self.name = 'a'
p = people()
p.m()
print(p.name)
# hello
# hello
# a
@property
是乙個裝飾器,將乙個方法當作屬性呼叫
@staticmethod
將方法定義成靜態方法,不需要傳入self
了
@classmethod
將乙個方法定義成類方法,傳入cls
而不是self
@abstraction
將乙個方法宣告成抽象方法
Python類中的方法(CLASS)
在類中可以根據需要定義一些方法,定義方法採用def關鍵字,在類中定義的方法至少會有乙個引數,一般以名為 self 的變數作為該引數 用其他名稱也可以 而且需要作為第乙個引數。舉例 class people sname xiaohong high 172.5 weight 180 男 def eat ...
Python中class的簡單介紹
類的定義 python class簡單操作 為了學習python中class,我們編寫乙個簡單的程式。我們的任務是找到stduents.txt檔案中,gpa最高的那名同學,並列印出他的資訊 其中students.txt檔案中的內容如下 zhangsan 127 228 lisi 100 400 wa...
Python學習筆記 class中的屬性
python3.x中沒有cmp函式了,定製sorted排序時候,不可以直接return cmp self.score,b.score 要進一步寫詳細。限制屬性種類 slots class person object slots name gender def init self,name,gende...