Examples of the properties and classes of objects in Python object programming

Source: Internet
Author: User
Properties of the Python object
To get to the point, take a look at an example of the differences between classes in Python, public properties in objects, private attributes and local variables, and global variables.

#!/usr/bin/env Python#coding:utf8 class Dave ():  var1 = "Class Atribute,public Atrribute var1" #类属性, public properties var1  __ VAR2 = ' class self atribute __var2 '  #类的私有属性__var2   def fun:    self.var2 = ' object public atrribute var2 ' #对 The public property of the elephant var2    self.__var3 = "Object self atrribute __var3" #对象的私有属性__var3    VAR4 = "Function of the local variable VAR4 "#函数fun的局部变量     def Other (self):    print Self.__var3

You can instantiate an object and get the class public property by adding the following code after the code above.

he = Dave ()    #实例化一个对象heprint he.var1   #从实例中获取类的公有属性print dave.var1  #直接从类中获取公有属性

Class Atribute,public Atrribute var1class atribute,public atrribute var1

The private property of a class cannot be called directly by a class or object

he = Dave () print Dave.__var2print he.__var2

Traceback (most recent): File "./object.py", line, in 
 
  
   
    print Dave.__var2attributeerror:class Dav E has no attribute ' __var2 '
 
  

But it can be called indirectly through methods.

Class Dave ():  var1 = "Class Atribute,public Atrribute var1" #类属性, public properties var1  __var2 = "class self Atribute __var2" 
   #类的私有属性__var2     def Other (self):    print dave.__var2    he = Dave () he.other ()

Class Self Atribute __var2

Getting the public property of an object in a class method requires that the method in the class be executed first through the object. The property is called through the object.

he = Dave () Liu = Dave () he.fun () print He.var2print liu.var2

       #对象liu由于没有调用fun方法所有就没有该属性. File "./object.py", line A, in 
 
  
   
    print Liu.var2AttributeError:Dave instance have no attribute ' var2 '
 
  

The private property of an object is similar to the private property of a class and cannot be called directly by a class or object

he = Dave () he.fun () print He.__var3

Traceback (most recent): File "./object.py", line +, in 
 
  
   
    print He.__var3attributeerror:dave Instan Ce has no attribute ' __var3 '
 
  

Local variables cannot be called directly by an object, and can be used inside a function.

he = Dave () he.fun () print HE.VAR4


Traceback (most recent): File "./object.py", line +, in 
 
  
   
    print He.var4AttributeError:Dave instance has no attribute ' VAR4 '
 
  

  def fun (self):    self.var2 = ' object public atrribute var2 ' #对象的公有属性var2    self.__var3 = ' object self atrribute __var3 "#对象的私有属性__var3    VAR4 =" function of the local variable VAR4 "#函数fun的局部变量    print VAR4       #可以在函数内部直接打印, which is only useful within this function C6/>print Self.__var3 he = Dave () he.fun ()

Function of the local variable var4object self atrribute __var3

So what's the difference between VAR4 and self._var3? At present, 2 are not used externally. The following defines a function other called.

  def fun (self):    self.var2 = ' object public atrribute var2 ' #对象的公有属性var2    self.__var3 = ' object self atrribute __var3 "#对象的私有属性__var3    VAR4 =" Function of the local variable VAR4 "#函数fun的局部变量    print VAR4       #一个函数的局部变量在另外一个函数是访问不到的    Print SELF.__VAR3     def other (self):    print var4    print Self.__var3 he = Dave () he.fun () print "#" * 100he.other ()

Function of the local variable var4object self atrribute __var3####################################################### ############################################ #Traceback (most recent):   #会认为var4是全局变量打印. Defining a global variable is available in the class Header Added VAR4 = "global" file "./object.py", line A, in 
 
  
   
    he.other () file "./object.py"  Print Var4nameerror:global name ' VAR4 ' is not defined
 
  

#!/usr/bin/env PYTHON#CODING:UTF8VAR4 = "global"            #定义var4为全局变量class Dave ():  var1 = "Class Atribute,public Atrribute var1 "#类属性, public attribute var1  __var2 = ' class self atribute __var2 '  #类的私有属性__var2   def fun (self):    SELF.VAR2 = "Object public atrribute var2" #对象的公有属性var2    self.__var3 = "Object self atrribute __var3" #对象的私有属性__var3 
  VAR4 = "Function of the local variable VAR4" #函数fun的局部变量    print var4    print SELF.__VAR3     def other (self): 
  print var4    Print self.__var3       #可调用私有属性 If you first call the fun he = Dave () he.fun () print "#" *100he.other ()

Function of the local variable var4object self atrribute __var3####################################################### ############################################ #globalobject Self Atrribute __var3

Methods of the Python class
Methods in the Python class: Public methods, private methods, class methods, static methods.

Here's an example of how they differ:

#!/usr/bin/env Python#coding:utf8class Dave ():  name = "Python"   def fun1 (self):        #定义公有方法    Print Self.name    print ' I am public method '   def __fun2 (self):       #定义私有方法    print self.name    print ' I am self Method

First look at the public and private methods, add the following code output

root@10.1.6.200:~#./method.py    #直接调用对象公有方法没有问题

Pythoni am Public method

Private methods, like private properties, are protected and cannot be called directly by private methods of an object, but can be called indirectly.

#!/usr/bin/env Python#coding:utf8class Dave ():  name = "Python"   def fun1 (self):        #定义公有方法    Print Self.name    print "I am public method"    self.__fun2 ()   def __fun2 (self):       #定义私有方法    Print Self.name    print "I am self method" he = Dave () he.fun1 ()

Pythoni am Public Methodpythoni am Self method

A public property can be called by a class, but the public method cannot be called directly by the class. The object invocation needs to be instantiated. If you want a method to be called directly by a class, you need to convert it into a class method. There are 2 kinds of methods that can be easily added to a class.

  @classmethod  def classfun (self):      #定义类方法    print self.name    print "I am class method" Dave.classfun ()

Pythoni am class method

Another way to be troublesome is to define a new function and use the Classmethod method to transform the function into a class method. Of course the call also needs to use the new function name.

  def classfun (self):      #定义类方法    print self.name    print "I am class method"   Classnewfun = Classmethod ( Classfun)  dave.classnewfun ()       #被转换后的是一个类方法, the original classfun is still a common method

Pythoni am class method

Static methods are used in the same way as class methods, and are intended to be called directly in the class, without self when the definition is distinguished.

  @staticmethod  def staticfun ():       #d定义静态方法    print Dave.name #注意不加self, calling a global variable requires using a Type plus property.    Print "I am static Method" Dave.staticfun ()

Pythoni am static method

The same can be done with a function call

  Def staticfun ():       #定义静态方法    print dave.name    print "I am static method"   Staticnewfun = Staticmethod ( Staticfun) Dave.staticnewfun ()

Pythoni am static method
  • 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.