An example is provided to illustrate the object attributes and class methods in Python surface object programming.

Source: Internet
Author: User

An example is provided to illustrate the object attributes and class methods in Python surface object programming.

Attributes of python objects
Next, let's take a look at an instance to learn about the differences between classes, public attributes, private attributes, local variables, and global variables in python.

root@10.1.6.200:~# cat object.py 
#! /Usr/bin/env python # coding: utf8 class Dave (): var1 = "class atri.pdf, public atrri#var1" # class attributes, public attribute var1 _ var2 = "class self atribute _ var2" # private attribute _ var2 def fun (self): self. var2 = "object public atrrisponvar2" # public attribute var2 self of the object. _ var3 = "object self atrribute _ var3" # private attribute of the object _ var3 var4 = "Function of the local variable var4" # def other (self): print self. _ var3

According to the code above, add the following code to instantiate an object and obtain public attributes of the class.

He = Dave () # instantiate an object heprint he. var1 # obtain the public attributes of the class from the instance print Dave. var1 # Get the public attributes directly from the class
root@10.1.6.200:~# ./object.py 
class atribute,public atrribute var1class atribute,public atrribute var1

Private attributes of a class cannot be directly called by a class or object.

he = Dave()print Dave.__var2print he.__var2
root@10.1.6.200:~# ./object.py 
Traceback (most recent call last): File "./object.py", line 19, in <module>  print Dave.__var2AttributeError: class Dave has no attribute '__var2'

But it can be called indirectly through methods.

Class Dave (): var1 = "class atri.pdf, public atrri#var1" # class attributes, public attribute var1 _ var2 = "class self atribute _ var2" # private attribute _ var2 def other (self): print Dave. _ var2 he = Dave () he. other ()
root@10.1.6.200:~# ./object.py 
class self atribute __var2

To obtain the public attribute of an object in a class method, you must first use the method in the object execution class and call this attribute through the object.

he = Dave()liu = Dave()he.fun()print he.var2print liu.var2
root@10.1.6.200:~# ./object.py 
Object public atrristmvar2traceback (most recent call last): <span> </span> # This attribute is not available because the object liu does not call the fun method. file ". /object. py ", line 20, in <module> print liu. var2AttributeError: Dave instance has no attribute 'var2'

The private attributes of an object are similar to those of a class, and cannot be directly called by a class or object.

he = Dave()he.fun()print he.__var3
root@10.1.6.200:~# ./object.py 
Traceback (most recent call last): File "./object.py", line 18, in <module>  print he.__var3AttributeError: Dave instance has no attribute '__var3'

Local variables cannot be directly called by objects. They can be used inside the function.

he = Dave()he.fun()print he.var4
root@10.1.6.200:~# ./object.py 


Traceback (most recent call last): File "./object.py", line 18, in <module>  print he.var4AttributeError: Dave instance has no attribute 'var4'
Def fun (self): self. var2 = "object public atrrisponvar2" # public attribute var2 self of the object. _ var3 = "object self atrribute _ var3" # private attribute of the object _ var3 var4 = "Function of the local variable var4" # print var4, a local variable of Function fun # Yes print directly inside the function, print self. _ var3 he = Dave () he. fun ()
root@10.1.6.200:~# ./object.py 
Function of the local variable var4object self atrribute __var3

So what is the difference between var4 and self. _ var3? Currently, neither of them can be used externally. The following defines a function called by other.

Def fun (self): self. var2 = "object public atrrisponvar2" # public attribute var2 self of the object. _ var3 = "object self atrribute _ var3" # private attribute of the object _ var3 var4 = "Function of the local variable var4" # print var4, a local variable of Function fun # the local variable of the function is the print self that cannot be accessed in another function. _ var3 def other (self): print var4 print self. _ var3 he = Dave () he. fun () print "#" * 100he. other ()
root@10.1.6.200:~# ./object.py 
Function of the local variable var4object self atrribute _ var3 ############################# ######################################## ############################## Traceback (most recent call last): # var4 is regarded as a global variable printing. you can add var4 = "global" File "to the class header to define global variables ". /object. py ", line 22, in <module> he. other () File ". /object. py ", line 16, in other print var4NameError: global name 'var4' is not defined
#! /Usr/bin/env python # coding: utf8var4 = "global" # define var4 as the global variable class Dave (): var1 = "class atri.pdf, public atrri#var1" # class attribute, public attribute var1 _ var2 = "class self atribute _ var2" # private attribute _ var2 def fun (self): self. var2 = "object public atrrisponvar2" # public attribute var2 self of the object. _ var3 = "object self atrribute _ var3" # private attribute of the object _ var3 var4 = "Function of the local variable var4" # print var4 print self, a local variable of Function fun. _ var3 def other (self): print var4 print self. _ var3 # You can call private attributes only when you call fun he = Dave () he. fun () print "#" * 100he. other ()
root@10.1.6.200:~# ./object.py 
Function of the local variable var4object self atrribute __var3####################################################################################################globalobject self atrribute __var3

Python Class Method
Methods In the python class: public methods, private methods, class methods, static methods.

The following example shows the differences between them:

#! /Usr/bin/env python # coding: utf8class Dave (): name = "python" def fun1 (self): # define the public method print self. name print "I am public method" def _ fun2 (self): # define the private method print self. name print "I am self method"

First, let's look at the Public and Private methods and add the following code to output them.

Root@10.1.6.200 :~ #./Method. py # No problem in directly calling the object public method
pythoni am public method

Private methods are protected like private properties. Private methods of objects cannot be called directly, but can be called indirectly.

#! /Usr/bin/env python # coding: utf8class Dave (): name = "python" def fun1 (self): # define the public method print self. name print "I am public method" self. _ fun2 () def _ fun2 (self): # define the private method print self. name print "I am self method" he = Dave () he. fun1 ()
root@10.1.6.200:~# ./method.py 
pythoni am public methodpythoni am self method

Public attributes can be called by classes, but public methods cannot be called directly by classes. the object to be instantiated is called. if you want to call a method directly by the class, you need to convert it into a class method. there are two ways to change to a class. You can add a decorator for simplicity.

@ Classmethod def classFun (self): # define the class method print self. name print "I am class method" Dave. classFun ()
root@10.1.6.200:~# ./method.py 
pythoni am class method

Another method is troublesome. You need to define a new function and use the classmethod method to convert the function into a class method. Of course, you also need to use the new function name to call the function.

Def classFun (self): # define the class method print self. name print "I am class method" classnewFun = classmethod (classFun) Dave. classnewFun () # The converted class method. classfun is still a common method.
root@10.1.6.200:~# ./method.py 
pythoni am class method

Static methods are used in the same way as class methods, so that they can be called directly in the class without self.

@ Staticmethod def staticFun (): # d defines the static method print Dave. name # Note: if you do not add self, you cannot directly call the name. You will think that you need to use the type and attribute to call the global variable. print "I am static method" Dave. staticFun ()
oot@10.1.6.200:~# ./method.py 
pythoni am static method

You can also call a function

Def staticfun (): # define the static method print Dave. name print "I am static method" staticnewFun = staticmethod (staticFun) Dave. staticnewFun ()
root@10.1.6.200:~# ./method.py 
pythoni am static method
Articles you may be interested in:
  • Python checks whether an object is a string class
  • Simple tutorial on defining, inheriting, and using Python classes
  • Analysis of object, method, class, instance, and function usage in Python
  • Detailed description of object-oriented programming in Python (II)
  • Detailed description of object-oriented programming in Python (Part 1)
  • Getting started with Python: Object-oriented
  • Object Type in Python
  • Attributes of Python objects for deep learning
  • Function objects in the python advanced tutorial (functions are also objects)
  • Python advanced tutorial
  • Practical Use of objects and classes in basic python tutorials
  • Basic python tutorial-object-oriented concepts

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.