Python----Application of object-oriented---binding method and unbound method

Source: Internet
Author: User

1. Application of methods bound to objects

1 classpeople:2     def __init__(self, name, age, Sex):3Self.name =name4Self.age = Age5Self.sex =Sex6 7     defTell_info (self):#methods to bind to an object8         Print('name:%s age:%s sex:%s'%(Self.name, Self.age, self.sex))9 Tenp = People ('Egon', 18,'male') OneP.tell_info ()#Tell_info (P) A  - The result is: -  theName:egon age:18 Sex:male

Bound to an object, it should be called by an object, automatically passing the object itself as the first parameter

2, binding to the class, it should be called by the class, automatically pass the class itself as the first parameter passed in

1 Importsetting2 3 classpeople:4     def __init__(self, name, age, Sex):5Self.name =name6Self.age = Age7Self.sex =Sex8 9     defTell_info (self):#methods to bind to an objectTen         Print('name:%s age:%s sex:%s'%(Self.name, Self.age, self.sex)) One  A @classmethod -     deffrom_conf (CLS): -obj =CLS ( the Setting.name, - Setting.age, - Setting.sex -         ) +         returnobj -  + #p = people (' Egon ', ' Male ') A #P.tell_info () # Tell_info (p) atp =people.from_conf () - P.tell_info () -  - The result is: -  -Name:xudachen age:18 Sex:male

3. Non-binding method

1 Importsetting2 ImportHashlib3 Import Time4 5 classpeople:6     def __init__(self, name, age, Sex):7Self.id =self.create_id ()8Self.name =name9Self.age = AgeTenSelf.sex =Sex One  A     defTell_info (self):#methods to bind to an object -         Print('name:%s age:%s sex:%s'%(Self.name, Self.age, self.sex)) -  the @classmethod -     deffrom_conf (CLS): -obj =CLS ( - Setting.name, + Setting.age, - Setting.sex +         ) A         returnobj at  - @staticmethod -     defcreate_id (): -m = hashlib.md5 (str (time.time ()). Encode ('Utf-8')) -         returnm.hexdigest () -  in #p = people (' Egon ', ' Male ') - #P.tell_info () # Tell_info (p) to #p = people.from_conf () + #P.tell_info () -  theP1 = People ('Xudachen1', 18,'male') *P2 = People ('Xudachen2', 28,'male') $P3 = People ('Xudachen3', 38,'male')Panax Notoginseng  - Print(p1.id) the Print(p2.id) + Print(p3.id) A  the The result is: +  - 1dadcd98fe26e278ce864c2f0cf2e753 $ bc09cd38527ef257fa3158d4e342ea1b $Bc09cd38527ef257fa3158d4e342ea1b

A non-binding method, not bound to a class or object, can be called, and no automatic value is passed.

Python----Application of object-oriented---binding method and unbound method

Related Article

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.