Python-class (5)

Source: Internet
Author: User

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 ############################4 #File Name:class5.py5 #Author:frank6 #Email: [Email protected]7 #Created time:2017-09-04 17:22:458 ############################9 Ten " " One class properties and Methods A  - private properties of the class - __private_attrs: Two underscores begin with, declaring that the property is private and cannot be used outside of the class or accessed directly. Self.__private_attrs when used in methods inside a class.  the  - methods of the class - inside a class, you can define a method for a class using the DEF keyword, unlike a generic function definition, which must contain the parameter self and be the first argument -  + private methods of the class - __private_method: Two underscores, declares that the method is a private method and cannot be called outside of a class. Calling Self.__private_methods inside a class + " " A  at " " - single underline, double underline, and tail-to-bottom double underline description: - __foo__: Defines a special method, similar to __init__ ().  - _foo: A variable of type protected that begins with a single underscore, that is, the protection type can only allow access to itself and subclasses, not to the From module import * - __foo: A double underline is a variable of a private type, which is allowed to be accessed only by the class itself.  - " " in  - classJustcounter: to     __secretcount= 0#Private Variables +Publiccount = 0#Exposing Variables -  the     defcount (self): *Self.__secretcount+ = 1 $Self.publiccount + = 1Panax Notoginseng         PrintSelf.__secretcount -Self.__mymth() the  +     def __mymth(self): A         Print "Private Method" the  +  -Counter =Justcounter () $ Counter.count () $ Counter.count () - #counter.__mymth () #AttributeError: justcounter instance has no attribute ' __mymth ' - PrintCounter.publiccount the #Print Counter.__secretcount # Attributeerror:justcounter instance has no attribute ' __secretcount ' - PrintCounter._justcounter__secretcount#Python does not allow instantiated classes to access private data, but you can use Object._classname__attrname to access properties

Python-class (5)

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.