The delivery of a set of class attributes (non-instance properties) between a parent class and a subclass in Python

Source: Internet
Author: User
Tags modifiers

A few days ago to do a project, meet a similar problem. The parent class is a common class, which is used in many sub-projects, and subclasses are used as a basic class in many parts of the project, but there are some class attributes in the original parent class (note that this is a class attribute, not a strength attribute). Re-set when the program is running.


Background: The class properties of the parent class in Python, the class method, the strength attribute all can inherit the quilt class, the Power attribute's re-setting is very simple, of course, in order to control the Class property access rights (Python can not fully control), you can also use the @preproty decorative character to optimize and control the strength of the property settings, The Class property of the parent class is inherited by the quilt class, which makes it easy to get the contents of the parent class property, but if you want to set the class property of the parent class, use the parent class name. class property name to implement, then can you use the subclass name. Class Property name to implement synchronization settings for class properties.


Take a look at an example:

Class Grandfather (object): MyList = [] def __init__ (self): Passclass Father (grandfather): Def __init__ (sel f): Passgrandfather.mylist = [1, 2, 3, 4]print grandfather.mylistprint Father.mylistFather.mylist = [' A ']print Gran Dfather.mylistprint father.mylist


Printing results:

[1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [' A ']

found that if you use

Father.mylist = [' A ']

To implement the set of class properties, imagine that the class properties of the parent class can be reset, but the results show that there is a gap between the imaginary and the reality.


I also tried using modifiers such as @preproty and @xxx.setter, and with @classmethod modifiers to implement synchronization settings with the Father.mylist implementation class properties, but the results failed. In fact, you can continue to use the Grandfather Class name plus class attributes and grandfather to implement the parent class property settings However, it feels obvious that there is a new parent class, alternately using the two class names in the module to set the class properties, is indeed a little too perfect.

Later, after trying to find a new method, is the use of metaclass, as for the content of Metaclass, we can find the corresponding article on the Internet, here are two ways to use.

The first type:

Class metamylist (Type):         def _get_dummy (self):         return Grandfather.mylist         def _set_dummy (Self, value):         Grandfather.mylist = value    mylist = property (_get_dummy, _set_ Dummy) Class grandfather (object):     mylist = []    def  __init__ (self):         passclass father (Grandfather):     __metaclass__ = metamylist    def __init__ (self):         passgrandfather.mylist = [1, 2, 3, 4]print  grandfather.mylistprint father.mylistfather.mylist = [' A ']print  Grandfather.mylistprint father.mylist

The results of the printing are satisfactory:

[1, 2, 3, 4] [1, 2, 3, 4] [' A '] [' A ']

Because we used __metaclass__ when we created the class, the class was created with MyList as its own class property, but when we used father.mylist to set the class properties, we were actually passing that value to grandfather. Because grandfather the class has been created, it is unrealistic to override the property of the class attribute (perhaps, but I read less). Then only when the creation of the father to override the property of such properties, and if you want to implement, it must use the class element, and Metaclass, which is simple, but it is really the ancestor of all classes.


The second method, and, of course, after understanding the first method, later found the second method when reading other articles. It's written here to make it easier for everyone to understand.

Class grandfather (object):     mylist = []    def _ _init__ (self):         passclass father (Grandfather):     class __metaclass__ (type):         @property          def mylist (CLS):             return grandfather.mylist        @ Mylist.setter        def mylist (cls, value):   #   @NoSelf             grandfather.mylist =  value    def __init__ (self):         passgrandfather.mylist = [1, 2, 3, 4]print grandfather.mylistprint  Father.mylistfather.mylist =&nBsp [' A ']print grandfather.mylistprint father.mylist


It's easy to see in Python that classes can be created dynamically in any legal place using legal indentation. In fact, the principle of the two methods is the same. But I personally prefer the first one, the code is more concise and crisp.


Caishuxueqian, welcome to the exchange of ideas and improve each other.


This article is from the "EIT Tramp" blog, please be sure to keep this source http://zicowarn.blog.51cto.com/3815716/1792735

The delivery of a set of class attributes (non-instance properties) between a parent class and a subclass in Python

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.