python動態建立屬性方法 tcy

2021-10-07 09:11:25 字數 3976 閱讀 9847

1.1.動態新增屬性方法:

方法1:用物件名.屬性名新增:p.age = 18

方法2:用setattr新增:if not hasattr(p,'age'):setattr(p,'age',18)

1.2.動態刪除屬性和方法:

方法1:del 物件.屬性名

方法2:delattr(物件,"屬性名")

1.3.__slots__限制例項屬性方法:

用途:限制類例項動態新增屬性;使依賴__dict__無效;提高**執行速度

注意:__slots__限制只對類例項起作用,對繼承子類不起作用

2.例項:

例項1:#動態新增屬性,方法

# 類定義

class student():

def __init__(self, name='tom'):

self.name = name

# 呼叫

s,s1 = student(),student()

# 1.1.動態新增屬性:

# 方法1:動態新增例項屬性:***

s.***='man'

# 方法2:通過動態新增例項方法間接繫結屬性:age

def setage(self, age):

self.age = age

from types import methodtype

s.setage = methodtype(setage, s) # 動態例項繫結方法

#方法3:動態新增類屬性

student.score=99

# 1.2.動態屬性測試:

s.setage(22)

s.name,s.***,s.age,s.score,student.score# ('tom', 'man', 22, 99, 99)

s.score=80

s.name,s.***,s.age,s.score,student.score#('tom', 'man', 22, 80, 99)

student.score=98

s.name,s.***,s.age,s.score,student.score# ('tom', 'man', 22, 80, 98)

s1.score# 98

# s1.age # attributeerror

# 2.1.1.動態新增例項方法:

def add(self,x,y):

return x+y

def sub(self,x,y):

return x-y

mul=lambda self,x,y:x*y

s.add = methodtype(add, s) #引數1為函式,引數2為例項物件

s.sub=sub # python不會自動將呼叫者繫結到引數1(self)需手動將呼叫者繫結為引數1(例項物件)

s.mul=mul # 能自動繫結引數1

# 測試:

s.add(2,3),s.sub(s,2,3),s.mul(s,2,3) #(5, -1, 6)

# s1.add(2,3),s1.sub(s1,2,3),s1.mul(s1,2,3)#都錯誤

# s.sub(2,3),s.mul(2,3) #都錯誤

# 2.1.2.動態新增all例項方法:

def add1(self,x,y):

return x+y

def sub1(self,x,y):

return x-y

mul1=lambda self,x,y:x*y

student.add1 = methodtype(add1, s)

student.sub1=sub1

student.mul1=mul1

# 測試:

s.add1(2,3),s.sub1(2,3),s.mul1(2,3) #(5, -1, 6)

student.add1(2,3),student.sub1(s,2,3),student.mul1(s,2,3) #(5, -1, 6)

student.add1(2,3),student.sub1(s1,2,3),student.mul1(s1,2,3)#(5, -1, 6)

# student.sub1(2,3),student.mul1(2,3) #都錯誤

# 2.2.動態新增類方法: 

@classmethod

def sport(cls,speed):

cls.score=80

return cls.score+speed

student.sport = sport

student.sport(100)#180

student.score # 80

# 2.3.動態新增靜態方法:  

@staticmethod

def draw(hourse):

return hourse

student.draw = draw

student.draw('draw hourse')#'draw hourse'

# 3.動態刪除屬性和方法:

print(hasattr(s,'score'),hasattr(s,'setage')) # true true 檢視屬性方法

del s.score;del student.score;delattr(s,'setage')# 刪除屬性方法

print(hasattr(s,'score'),hasattr(s,'setage')) # false false

setattr(s,'score',80);setattr(student,'score',88) # 動態建立屬性

s.setage = methodtype(setage, s) # 動態建立方法

print(hasattr(s,'score'),hasattr(s,'setage')) # true true

例項2:#__slots__限制例項屬性方法

# 類定義

class boy(object):

__slots__ = ('name', 'age','foo') # 限制例項屬性方法僅能為name,age,foo

def __init__(self):

pass

# 呼叫

s = boy()

s.name = 'tom' # 繫結屬性'name'

s.age = 25 # 繫結屬性'age'

#s.score = 99 # 繫結屬性'score' 引發attributeerror

fx=lambda self,x,y:x+y

s.foo = lambda self,x,y: print(self.name,x,y) #例項方法slots中一定要有該名稱

bar= lambda self,x,y: print(self.name,x,y) #類方法,slots不必有

boy.bar =bar

s.bar(2,3),bar(s,2,3) # tom 2 3 tom 2 3

s.foo(s,2,3) #tom 2 3

python 動態繫結屬性方法

import types 定義類 class person object num 0 def init self,name,age none self.name name self.age age def eat self print eat food 定義例項方法 def run self,spe...

python動態新增屬性和方法

class person def init self,name,age self.name name self.age age p1 person ff 28 print p1.name,p1.age 給例項物件動態新增 屬性 p1.female print p1.給類動態新增屬性 person.h...

Python動態新增屬性和方法

動態新增屬性,就是這個屬性不是在類定義的時候新增的,而是在程式執行過程中新增的,動態新增屬性有兩種方法,第乙個是直接通過物件名.屬性名,第二個是通過setattr新增 1 第一種 使用物件.屬性名新增 p.ageb 18 2 第二種,使用setattr函式新增 class person def in...