一、动态语⾔的定义

动态语言是在运行时确定数据类型的语言。变量使用之前不需要类型声明,通常变量的类型是被赋值的那个值的类型。现在比较热门的动态语言有:Python、PHP、JavaScript、Objective-C等,而 C 、 C++ 等语言则不属于动态语言。

二、运行的过程中给对象绑定(添加)属性

class Person(object):
    def __init__(self,name=None,age=None):
        self.name=name
        self.age=age

p = Person("小明","24")
print(p.name)
print(p.age)

运行结果为:

小明
24

这里我们只定义了name和age两个属性,但是在类已经定义好了之后,我们仍然可以往里面添加属性,这就是动态语言的好处,动态的给实例绑定属性:

class Person(object):
    def __init__(self,name=None,age=None):
        self.name=name
        self.age=age

p = Person("小明","24")
print(p.name)
print(p.age)

#动态添加属性
p.sex = ""
print(p.sex)

运行结果为:

小明
24
男

三、运行的过程中给类绑定(添加)属性

class Person(object):
    def __init__(self,name=None,age=None):
        self.name=name
        self.age=age


P1 = Person("小明",24)
print(P1.sex)

运行结果为:

Traceback (most recent call last):
  File "C:\Users\Se7eN_HOU\Desktop\test.py", line 8, in <module>
    print(P1.sex)
AttributeError: 'Person' object has no attribute 'sex'

这是程序报错说,Person没有sex这个属性,我们可以通过给Person动态绑定属性,解决问题

class Person(object):
    def __init__(self,name=None,age=None):
        self.name=name
        self.age=age


P1 = Person("小明",24)
#动态给类添加属性
Person.sex = ""
print(P1.sex)

这个时候在运行就不会出错,而且会打印出P1.sex为男

四、运行的过程中给类绑定(添加)方法

class Person(object):
    def __init__(self,name=None,age=None):
        self.name=name
        self.age=age

    def eat(self):
        print("正在吃东西")

P1 = Person("小明",24)
P1.eat()
P1.run()

运行结果为:

正在吃东西
Traceback (most recent call last):
  File "C:\Users\Se7eN_HOU\Desktop\test.py", line 11, in <module>

    P1.run()
AttributeError: 'Person' object has no attribute 'run'

说明:正在吃东西打印出来了,说明eat函数被执行,但是后面报错说没有run这个属性,但是我想在类创建好了以后,在运行的时候动态的添加run方法怎么办呢?

#动态添加方法需要导入types模块
import types
class Person(object):
    def __init__(self,name=None,age=None):
        self.name=name
        self.age=age

    def eat(self):
        print("正在吃东西")

#定义好需要动态添加的方法
def run(self):
    print("在跑步")

P1 = Person("小明",24)
#正常调用类里面的函数
P1.eat()

#给对象动态绑定方法
P1.run = types.MethodType(run,P1)
#对象调用动态绑定的方法
P1.run()

运行结果为:

正在吃东西
在跑步

打印出来“在跑步”说明run方法被正常执行了

动态绑定类方法和静态方法

#动态添加方法需要导入types模块
import types
class Person(object):
    def __init__(self,name=None,age=None):
        self.name=name
        self.age=age

    def eat(self):
        print("正在吃东西")

#定义好需要动态添加的实例方法
def run(self):
    print("在跑步")

#定义好需要动态添加的类方法
@classmethod
def dynamicClassMethod(cls):
    print("这是一个动态添加的类方法")
#定义好需要动态添加的静态方法
@staticmethod
def dynamicStaticMethod():
    print("这是一个动态添加的静态方法")

P1 = Person("小明",24)
#正常调用类里面的函数
P1.eat()

#给对象动态绑定方法
#MethodType(参数1,参数2)
#参数1:是动态绑定哪个方法,只写方法名即可
#参数2:是把这个方法动态的绑定给谁
P1.run = types.MethodType(run,P1)
P1.run()

#动态绑定类方法的使用
Person.dynamicClassMethod = dynamicClassMethod
Person.dynamicClassMethod()

#动态绑定静态方法的使用
Person.dynamicStaticMethod = dynamicStaticMethod
Person.dynamicStaticMethod()

总结:

  1. 给对象绑定属性直接在使用前进行赋值使用即可
  2. 给对象动态绑定方法需要import types模块
  3. 给对象动态绑定实例方法,需要使用type.MethodType()方法
  4. 给类添加类方法和静态方法,也是直接在使用前赋值即可使用

五、运行的过程中删除属性、方法

删除的方法:

  1. del 对象.属性名
  2. delattr(对象, "属性名")
class Person(object):
    def __init__(self,name=None,age=None):
        self.name=name
        self.age=age

P1 = Person("小明",24)
print("---------删除前---------")
print(P1.name)

del P1.name

print("---------删除后---------")
print(P1.name)

运行结果为:

---------删除前---------
小明
---------删除后---------
print(P1.name)AttributeError: 'Person' object has no attribute 'name'

六、__slots__

动态语言:可以在运行的过程中,修改代码

静态语言:编译时已经确定好代码,运行过程中不能修改

如果我们想要限制实例的属性怎么办?比如,只允许对Person实例添加name和age属性。

为了达到限制的目的,Python允许在定义class的时候,定义一个特殊的__slots__变量,来限制该class实例能添加的属性:

class Person(object):
    __slots__=("name","age")

p = Person()
p.name = "老王"
p.age = 40
print(p.name)
print(p.age)

#slots之外的属性
p.sex = ""
print(p.sex)

运行结果为:

老王
40
    p.sex = ""
AttributeError: 'Person' object has no attribute 'sex'

注意:

  • 使用__slots__要注意,__slots__定义的属性仅对当前类实例起作用,对继承的子类是不起作用的
04-18 12:46