1.2. Python is a dynamic language

Source: Internet
Author: User
Tags instance method

Python is dynamic language 1. Definition of dynamic language

动态编程语言is 高级程序设计语言 a category that has been widely used in the field of computer science. It is a class 运行时可以改变其结构 of languages: for example, new functions, objects, even code can be introduced, existing functions can be deleted or other structural changes. Dynamic languages are now very vibrant. For example, JavaScript is a dynamic language, in addition to PHP, Ruby, Python and so on are also dynamic languages, and C, C + + and other languages are not dynamic language. ----from Wikipedia

2. Binding (adding) properties to an object during run
class Person (object):     def __init__ (self, name = none, age = None):         = name        = age >>> P = person (" xiaoming ""24  ")>>>

Here, we define 1 class person, in this class, define two initial properties name and age, but people still have sex! If this class was not written by you, would you try to access the gender attribute?

" male ">>> p.sex'male'>>>

At this point, we find the problem, we define the class there is no sex this attribute Ah! What's going on here? This is the charm and pit of dynamic language! This is actually the dynamic to the instance binding Property !

3. Binding (adding) properties to a class during run
>>> P1 = person (" Xiao Li """")>>>  P1.sextraceback (most recent):  '<pyshell#21>' in <module>    'sex'>>>

We tried to print P1.sex, found an error, P1 no sex this attribute! ----give p this instance binding property does not work for P1! So what do we do with the sex attribute for all the instances of the person? The answer is to bind the attribute directly to the person!

# add a property to the class person >>> P1 = person (" Xiao Li """ Print# If P1 does not have a sex attribute in this instance object, then it accesses its Class property # to see that no exception occurred >>>
4. Bind (Add) methods to classes during run

We directly bind to person sex This property, after re-instantiation of P1, P1 has sex this attribute! What about function? How to bind?

>>>classPerson (object):def __init__(self, name = None, age =None): Self.name=name Self.age= AgedefEat (self):Print("Eat Food")>>>defrun (self, speed):Print("%s is moving at a speed of%d/h"%(Self.name, speed))>>> P = Person ("Lao Wang", 24)>>>p.eat () Eat food>>> >>>P.run () Traceback (most recent): File"<pyshell#5>", Line 1,inch<module>P.run () Attributeerror:person instance has no attribute'Run'>>>>>>>>>ImportTypes>>> P.run =types. Methodtype (Run, P)>>> P.run (180The old King was moving, and the speed was+/-H

Since adding a method to a class is used 类名.方法名 = xxxx , then adding a method to the object is also a class对象.方法名 = xxxx

The complete code is as follows:
ImportTypes#defines a classclassPerson (object): Num=0def __init__(self, name = None, age =None): Self.name=name Self.age= AgedefEat (self):Print("Eat Food")#defining an instance methoddefrun (self, speed):Print("%s is moving at a speed of%d/h"%(Self.name, speed))#defining a class method@classmethoddefTestClass (CLS): Cls.num= 100#define a static method@staticmethoddefteststatic ():Print("---static method----")#Create an Instance objectP = Person ("Lao Wang", 24)#methods that are called in classp.eat ()#add an instance method to this objectP.run =types. Methodtype (Run, P)#Invoking instance methodsP.run (180)#bind class method to Person classPerson.testclass =TestClass#Calling class methodsPrint(Person.num) person.testclass ()Print(Person.num)#to bind a static method to the person classPerson.teststatic =teststatic#calling a static methodPerson.teststatic ()
5. Delete properties, methods during run

method to delete:

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

A conclusion can be drawn from the above example: static language is rigorous with respect to dynamic language! So, when playing dynamic language, watch out for dynamic pits!

So how to avoid this situation? Please use __slots__,

1.2. Python is a dynamic language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.