Python study note 5 (Object-Oriented-class-method-attribute)

Source: Internet
Author: User

All of the items listed here are from the C # perspective and may not be appropriate. However, it is easier to understand abstract concepts.

Function Definition
Class
There are no public, private, or protect classes in Python.

Constructor and destructor
_ Init _ (Self)
_ Del _ (Self)

Class static variables
Class student
Name = "ABC"
This is actually equivalent to static variables in C #, but note that,Static variable of initialization classYes (example in diveappspython)
Class counter:
Count = 0
Def _ init _ (Self ):
Self. _ class _. Count + = 1

Instance member variables
Class student
Def _ init _ (Self)
Self. Name = "ABC"

attribute definition (the class inherits from the object, but it does not seem necessary, because the following Code can also be used properly)
class student:
def _ init _ (Self ):
self. _ name = "ABC"
def getname (Self):
return self. _ name
def setname (self, value):
self. _ name = value
def delname (Self):
Del self. _ name
name = property (getname, setname, delname, 'student name')
to be honest, when I finish writing this code, I don't want to use attributes anymore. This definition is too inconvenient, and it also needs to be inherited from the object. In C #, you can easily implement attributes through get and set. No good reason for using attributes is found currently

Read-only attribute(The class must inherit from the object, otherwise it is not read-only)
Class parrot (object ):
Def _ init _ (Self ):
Self. _ voltage = 100000
@ Property
Def voltage (Self ):
"Get the current voltage ."""
Return self. _ voltage

Private variable
Class student:
Def _ init _ (Self ):
Self. _ name = "ABC"
It is easy to start with _, but not the end. Private

Private Method
Class student:
Def _ getage (Self ):
Pass
Like a private variable, you can call it directly. The compiler will prompt you accordingly.

Force access to private methods and variables
"Private" is actually a rule. We can still use special syntax to access private members.
The above method can be accessed through the _ class name.
Aobj = student ()
Aobj. _ student _ name
Aobj. _ student _ getage ()

Static Method
Class class1:
@ Staticmethod
Def test ():
Print "in static method ..."

Method overload
Python does not support method overloading, But you can write your code. Python will use the last one. I think this may be related to storing the information in Python through _ dict _, which is a dict. Keys cannot be the same. So there is no way to call two Gogo methods.

Class student:
Def Gogo (self, name ):
Print Name
Def Gogo (Self ):
Print "default"

You can only use the obj. Gogo () method when calling.

Some special methods

_ Init _ (Self) constructor
_ Del _ (Self) destructor
_ Repr _ (Self) repr ()
_ STR _ (Self) print statement or STR ()

Operator overload
_ LT _ (self, other)
_ Le _ (self, other)
_ Eq _ (self, other)
_ Ne _ (self, other)
_ GT _ (self, other)
_ Ge _ (self, other)

There are too many things. Let's take a look at Python's built-in help.

Some special attributes
Some default attributes that can be obtained when you define a class and call class instance
Class student:
'''This test class '''
Name = 'ss'
Def _ init _ (Self ):
Self. Name = 'bb'
Def run (Self ):
'''People run '''
@ Staticmethod
Def runstatic ():
Print "in static method ..."
Print student. _ dict _ # member information of the class
Print student. _ Doc _ # class description
Print student. _ name _ # Class Name
Print student. _ module _ # module of the class
Print student. _ bases _ # class inheritance Information
OBJ = student ()

Print Dir (OBJ)
Print obj. _ dict _ # member variable information of the Instance (I don't quite understand the python model. Why does the run function no longer exist in dict)
Print obj. _ Doc _ # instance description
Print obj. _ module _ # module of the Instance
Print obj. _ class _ # Class Name of the Instance

ReferenceArticle:

Http://www.xwy2.com/article.asp? Id = 119

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.