day8-python-member modifier

Source: Internet
Author: User
Tags print object

Python's face object, with 3 Members, fields, methods, properties

1 classFoo:2     def __init__(self,name):3         #The public field name can be called in the class and outside of the =name5 6     defF1 (self):7         Print( 9obj = Foo ('Alex')Ten #Internal Call One Print( A #>>>alex - #External calls - obj.f1 () the #>>>alex

1 classFoo:2     def __init__(self,name):3         #private field name cannot be called except in class calling class4Self.__name=name5 6     defF1 (self):7         Print(self.)__name)8 9 Tenobj = Foo ('Alex') One #Internal Call A Print( - #>>>alex - #External calls the obj.f1 () - #>>> Error
1 #Special Members2 classFoo:3     #Construction Method4     def __init__(self,name,age) =name6Self.age = Age7         Print('executing when instantiating an object')8 9     #destructor MethodTen     def __def__(self): One         Print('automatically triggers execution when memory is released') A     def __call__(self): -         Print('Pager') -      the     def __str__(self): -         return '%s-%d'%(,self.age) -         return 'Show me what you return' -p=Foo () +>>>executing when instantiating an object - #>>> Show which class the object belongs to + Print(p.__class__) A>>><class '__main__. Foo'> at #Execute The call method - p () ->>>Pager - #The constructor method is executed before the call method is executed - Foo () () ->>>executing when instantiating an object in>>>Pager -  toObj1 = Foo ('Alex', 71) +Obj2 = Foo ('Eric', 72) - #The print object automatically calls the __str__ method the Print(OBJ1) *>>>alex-71 $ Print(OBJ2)Panax Notoginseng>>>eric-72 - #automatically invoke the __str__ method when executing the str () method theRET =Str (OBJ1) + Print(ret) A>>>alex-71

day8-python-member modifier

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: 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.