Python object-oriented programming, python object-oriented

Source: Internet
Author: User

Python object-oriented programming, python object-oriented

Python Object-Oriented Programming

A class object can have the following data types:

1. Static variables

2. dynamic variables

3. Private Variables

4. Static Method

5. Dynamic Methods

6. Private methods

7. Class Methods

8. Features

9. proprietary methods


First, define a class:

#! /Usr/bin/env python # coding: utf-8class Province (object): # static field desc = 'China' # dynamic field def _ init _ (self, name, capital, leader, flag = True): # define the proprietary method _ func _ # dynamic field. Only the instantiated object can access the dynamic field. The class itself cannot access the dynamic field self. name = name self. capital = capital self. leader = leader self. _ Pollution = flag # private variable definition _ var self. dict = {'name': 'lil', 'age': 30} # dynamic method, or object method. Only the instantiated object can access the dynamic method def FrankDynamic (self ): print 'this is a dynamic method' # The static method cannot The usage variables and instance variables are equivalent to a global function # You can use this method without defining an instance. In addition, multiple instances share this static method. You only need to allocate one memory @ staticmethod def LeeStatic (): print 'this is static method' # feature, the access method is the same as the field access method. @ property def AllenProperty (self): return self. name # class method: A class method can be called through a class object or an instance of a Class Object. # Whether you call this method using a class object or a class instance, the first parameter of This method always defines the class object of This method @ classmethod def MarlonClass (cls): print 'this is class method' # private function, it can only be used in dynamic functions of the class, or through the instance object. _ Province _ sha is called, but do not use def _ sha (self): print 'this is Private function 'def AswillDynamic (self ): # You can call the private function self through a dynamic method. _ sha () @ property # Use the feature to access dynamic private fields. You can use this modifier, also known as the feature def Pollution (self): print self. _ Pollution @ Pollution. setter # modify the dynamic private field through the feature. You can use this modifier. It must be combined with the preceding function def Pollution (self, value): self. _ Pollution = value # commonly used proprietary method def _ call _ (self ): #__ call _ private method print "this is call method" return "this is _ str _ private method" def _ str _ (self ): return "this is _ str _ proprietary method" def _ getitem _ (self, key): return self. dict [key] def _ setitem _ (self, key, value): self. dict [key] = value def _ delitem _ (self, key): del self. dict [key] #################### the above section describes the definition of a class ########### ###################

Then instantiate this class and use various data types

# Instantiate an object HeiBeiProvince = Province ('hebei ', 'shijiazhuang', 'Li yang', flag = False) # The object can access the static field print HeiBeiProvince. descHeiBeiProvince. frankDynamic () # The class cannot access the dynamic method # Province. frankDynamic () # objects and classes can access the static method LeeStaticProvince. leeStatic () HeiBeiProvince. leeStatic () # feature access method print HeiBeiProvince. allenProperty # feature, which can be accessed like access variables # how to access the private function HeiBeiProvince. aswillDynamic () # This public function calls the object's private function _ sha () # modifies the private variable HeiBeiProvince through feature access. pollutionHeiBeiProvince. pollution = FalseHeiBeiProvince. pollutionHeiBeiProvince. marlonClass () # The object can use the following method to evaluate the private function HeiBeiProvince of the class. _ Province _ sha () # private functions can be accessed in this way, but are never used in Programs # use of proprietary methods #__ call __, you can use the class object as a function using HeiBeiProvince () # Use the class object as a function, which is equivalent to executing HeiBeiProvice. _ call __() #__ str _ print HeiBeiProvince # The Private method _ str __#__ getitem _ print HeiBeiProvince ['name'] #__ setitem _ print 'before _ setitem _ _', heiBeiProvince ['name'] HeiBeiProvince ['name'] = 'Frank 'print 'after _ setitem __', heiBeiProvince ['name'] #__ delitem _ del HeiBeiProvince ['name'] print HeiBeiProvince. dict





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.