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)