83 python Advanced - python Dynamic Language

Keywords: Attribute Programming Javascript PHP

1. Definition of dynamic language

Dynamic programming language is a category of advanced programming languages and has been widely used in the field of computer science.It is a language that can change its structure at run time: new functions, objects, even code can be introduced, existing functions can be deleted, or other structural changes.Dynamic languages are currently very dynamic.JavaScript, for example, is a dynamic language. In addition, PHP, Ruby, Python and other languages are also dynamic languages, while C, C++ and other languages are not.- From Wikipedia

2. Bind (add) properties to objects during run

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


>>> P = Person("Xiao Ming", "24")
>>>

Here, we define a class Person, in which we define two initial attributes, name and age, but people also have sex!If you didn't write this type, would you try to access the attribute of gender?

>>> P.sex = "male"
>>> P.sex
'male'
>>>

At this time, we found a problem. There is no sex attribute in the class we defined.What's going on?This is the charm and pit of dynamic language!This is actually the dynamic binding of properties to the instance!

3. Bind (add) properties to classes during run

>>> P1 = Person("Xiao Li", "25")
>>> P1.sex

Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    P1.sex
AttributeError: Person instance has no attribute 'sex'
>>>

We tried to print P1.sex and found an error. P1 has no sex attribute!- - Binding properties to the instance P will not work on the instance P1!So what do we do to add the sex attribute to all instances of Person?The answer is to bind properties directly to Person!

>>>> Person.sex = None #Add an attribute to the class Person
>>> P1 = Person("Xiao Li", "25")
>>> print(P1.sex) #If P1 does not have a sex attribute in this instance object, then its class attribute will be accessed
None #You can see that no exceptions occurred
>>>

4. Bind (add) methods to classes during run

We bind the property sex directly to Person, and after re-instantiating P1, P1 will have the property sex!What about function?How do I bind?

>>> 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 Moving, Speed is %d km/h"%(self.name, speed))


>>> P = Person("King", 24)
>>> P.eat()
eat food
>>> 
>>> P.run()
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    P.run()
AttributeError: Person instance has no attribute 'run'
>>>
>>>
>>> import types
>>> P.run = types.MethodType(run, P)
>>> P.run(180)
//Lao Wang is moving,Speed is 180 km/h

Since adding a method to a class uses the class name.Method name= xxxx, adding a method to an object is a similar object.Method name= xxxx.

The complete code is as follows:

import types

#A class is defined
class Person(object):
    num = 0
    def __init__(self, name = None, age = None):
        self.name = name
        self.age = age
    def eat(self):
        print("eat food")

#Define an instance method
def run(self, speed):
    print("%s Moving, Speed is %d km/h"%(self.name, speed))

#Define a class method
@classmethod
def testClass(cls):
    cls.num = 100

#Define a static method
@staticmethod
def testStatic():
    print("---static method----")

#Create an instance object
P = Person("King", 24)
#Call methods in class
P.eat()

#Add instance methods to this object
P.run = types.MethodType(run, P)
#Call Instance Method
P.run(180)

#Bind Class Method to Person Class
Person.testClass = testClass
#Call Class Method
print(Person.num)
Person.testClass()
print(Person.num)

#Bind static method to Person class
Person.testStatic = testStatic
#Call static methods
Person.testStatic()

5. Delete attributes and methods while running

Method to delete:

  1. del object. Property name
  2. Delattr (object, "property name")

From the above examples, it can be concluded that static languages are more rigorous than dynamic languages!So, when you play dynamic language, be careful of dynamic pits!

So how can you avoid this?Please use u slots_u (described in the next section),

Posted by hassanz25 on Sun, 18 Aug 2019 18:39:18 -0700