python动态_python的动态性和_slot_
python是動態語言
1. 動態語言的定義
動態編程語言?是?高級程序設計語言?的一個類別,在計算機科學領域已被廣泛應用。它是一類 在運行時可以改變其結構的語言 :例如新的函數、對象、甚至代碼可以被引進,已有的函數可以被刪除或是其他結構上的變化。動態語言目前非常具有活力。例如JavaScript便是一個動態語言,除此之外如 PHP 、 Ruby 、 Python 等也都屬于動態語言,而 C 、 C++ 等語言則不屬于動態語言。----來自 維基百科
2. 運行的過程中給對象綁定(添加)屬性
>>> class Person(object):
def __init__(self, name= None, age =None):
self.name=name
self.age=age>>> P = Person("小明", "24")>>>
在這里,我們定義了1個類Person,在這個類里,定義了兩個初始屬性name和age,但是人還有性別啊!如果這個類不是你寫的是不是你會嘗試訪問性別這個屬性呢?
>>> P.sex = "male"
>>>P.sex'male'
>>>
這時候就發現問題了,我們定義的類里面沒有sex這個屬性啊!怎么回事呢? 這就是動態語言的魅力和坑! 這里 實際上就是 動態給實例綁定屬性!
3. 運行的過程中給類綁定(添加)屬性
>>> P1 = Person("小麗", "25")>>>P1.sex
Traceback (most recent call last):
File"", line 1, in P1.sex
AttributeError: Person instance has no attribute'sex'
>>>
我們嘗試打印P1.sex,發現報錯,P1沒有sex這個屬性!---- 給P這個實例綁定屬性對P1這個實例不起作用! 那我們要給所有的Person的實例加上 sex屬性怎么辦呢? 答案就是直接給Person綁定屬性!
>>>> Person.sex =None #給類Person添加一個屬性>>> P1 = Person("小麗", "25")>>>print(P1.sex) #如果P1這個實例對象中沒有sex屬性的話,那么就會訪問它的類屬性
None #可以看到沒有出現異常>>>
4. 運行的過程中給類綁定(添加)方法
我們直接給Person綁定sex這個屬性,重新實例化P1后,P1就有sex這個屬性了! 那么function呢?怎么綁定?
>>> class Person(object):
def __init__(self, name= None, age =None):
self.name=name
self.age=age
def eat(self):
print("eat food")>>>def run(self, speed):
print("%s在移動, 速度是 %d km/h"%(self.name, speed))>>> P = Person("老王", 24)>>>P.eat()
eat food>>>
>>>P.run()
Traceback (most recent call last):
File"", line 1, in P.run()
AttributeError: Person instance has no attribute'run'
>>>
>>>
>>>import types>>> P.run =types.MethodType(run, P)>>> P.run(180)
老王在移動,速度是 180 km/h
既然給類添加方法,是使用類名.方法名 = xxxx,那么給對象添加一個方法也是類似的對象.方法名 = xxxx
完整的代碼如下:
import types
#定義了一個類class Person(object):
num= 0def __init__(self, name= None, age =None):
self.name=name
self.age=age
def eat(self):
print("eat food")
#定義一個實例方法
def run(self, speed):
print("%s在移動, 速度是 %d km/h"%(self.name, speed))
#定義一個類方法
@classmethod
def testClass(cls):
cls.num= 100#定義一個靜態方法
@staticmethod
def testStatic():
print("---static method----")
#創建一個實例對象
P= Person("老王", 24)
#調用在class中的方法
P.eat()
#給這個對象添加實例方法
P.run=types.MethodType(run, P)
#調用實例方法
P.run(180)
#給Person類綁定類方法
Person.testClass=testClass
#調用類方法
print(Person.num)
Person.testClass()
print(Person.num)
#給Person類綁定靜態方法
Person.testStatic=testStatic
#調用靜態方法
Person.testStatic()
5. 運行的過程中刪除屬性、方法
刪除的方法:
del 對象.屬性名
delattr(對象, "屬性名")
通過以上例子可以得出一個結論:相對于動態語言,靜態語言具有嚴謹性!所以,玩動態語言的時候,小心動態的坑!
那么怎么避免這種情況呢? 請使用__slots__,
__slots__
現在我們終于明白了,動態語言與靜態語言的不同
動態語言:可以在運行的過程中,修改代碼
靜態語言:編譯時已經確定好代碼,運行過程中不能修改
如果我們想要限制實例的屬性怎么辦?比如,只允許對Person實例添加name和age屬性。
為了達到限制的目的,Python允許在定義class的時候,定義一個特殊的__slots__變量,來限制該class實例能添加的屬性:
>>> class Person(object):
__slots__= ("name", "age")>>> P =Person()>>> P.name = "老王"
>>> P.age = 20
>>> P.score = 100Traceback (most recent call last):
File"", line 1, in AttributeError: Person instance has no attribute'score'
>>>
注意:
使用__slots__要注意,__slots__定義的屬性僅對當前類實例起作用,對繼承的子類是不起作用的
In [67]: classTest(Person):
...:pass...:
In [68]: t =Test()
In [69]: t.score = 100
總結
以上是生活随笔為你收集整理的python动态_python的动态性和_slot_的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初始化模型参数 python_pytor
- 下一篇: ubuntu ifconfig命令找不到