Python Learning Day13 Python object-oriented Learning 2: @property, multiple inheritance

Source: Internet
Author: User

Using @property

When binding properties, if we directly expose the properties, although it is very simple to write, but there is no way to check the parameters, resulting in the result can be changed casually:

s == 9999

This is obviously illogical. To limit the scope of the score, you can set the score by one method, and set_score() then get the score by one, get_score() so that in the set_score() method, you can check the parameters:

classStudent (object):defGet_score (self):returnSelf._scoredefSet_score (self, value):if  notisinstance (value, int):RaiseValueError ('score must is an integer!')        ifValue < 0orValue > 100:            RaiseValueError ('score must between 0 ~ 100!') Self._score= value

Now, to operate on any of the student instances, you cannot set score as you wish:

>>> s = Student ()#  ok! >>> s.get_score ()60>>> S.set_score (9999) Traceback (most recent call last):  ~ 100!

However, the above method of invocation is slightly more complex, and it is not straightforward to use attributes directly.

Is there any way to access the variables of a class in such an easy manner as to check parameters and use similar properties? For the perfect Python programmer, this must be done!

Remember the adorner (decorator) can add functionality to a function dynamically? For class methods, adorners work as well. Python's built-in @property decorator is responsible for turning a method into a property invocation :

classStudent (object): @propertydefscore (self):returnself._score @score. Setterdefscore (self, value):if  notisinstance (value, int):RaiseValueError ('score must is an integer!')        ifValue < 0orValue > 100:            RaiseValueError ('score must between 0 ~ 100!') Self._score= value

@propertyImplementation is more complex, we first examine how to use. To turn a getter method into a property, just add it, and @property at this point, it @property creates another adorner @score.setter , which is responsible for turning a setter method into a property assignment, so we have a controllable property operation:

>>> s = Student ()#  OK, actual conversion to S.set_score#  OK, actual conversion to S.get_score () 60>>> s.score = 9999Traceback (most recent call last):  ~ 100!

Notice that this magical @property , when we operate on instance properties, we know that this property is probably not directly exposed, but is implemented through getter and setter methods.

You can also define read-only properties, define getter methods only, and do not define setter methods as a read-only property :

class Student (object):    @property    def  Birth (self):        return  self._ Birth    @birth. Setter    def  Birth (self, value):        = value    @property     def Age (self):         return 2015-self._birth

The above birth is a read- write property , which age is a readonly property because age it can be calculated based on the birth current time.

Summary

@propertyWidely used in the definition of a class, you can let the caller write short code, while ensuring that the parameters are necessary to check, so that the program runs to reduce the likelihood of errors.

Python Multiple Inheritance (mixin mode)

Languages such as C or C + + support multiple inheritance, and a subclass can have more than one parent, a design that is often criticized. Because inheritance should be a "is-a" relationship. Cars, for example, inherit the vehicle class, because the sedan is a ("is-a") vehicle. An item cannot be many different things, so there should be no multiple inheritance. But is there a case where a class does need to inherit multiple classes?

The answer is yes, we still take the transportation to give example, the civil aircraft is a kind of transportation, for local tyrants The helicopter is also a kind of transportation. For both modes of transport, they all have a function of flying, but the sedan does not. Therefore, we can not write the flight function in the vehicle of the parent class. But if both civil aircraft and helicopters write their own flight methods, they violate the principle of reusing the code as much as possible (if there are more flying tools later, there will be a lot of duplicate code). What to do, then we have to let the two planes inherit the vehicle and the aircraft two of the parent class, so there are multiple inheritance. At this time also violated the inheritance must be "is-a" relationship. How can this puzzle be broken?

Different languages give a different approach, let's look at Java first. Java provides interface interface functionality to implement multiple inheritance :

class Vehicle {} public interface flyable {    class  Flyableimpl implements flyable {public    void FL Y () {        System.out.println ("I am Flying");     class airplane extends Vehicle implements flyable {    private flyable;     Public airplane () {        = new Flyableimpl ();    }     public void Fly () {        flyable.fly ();    }}

Now our aircraft has both the vehicle and the vehicle properties, and we do not need to rewrite the flight methods in the aircraft, and we do not undermine the principle of single inheritance. Aircraft is a means of transport, the ability to fly is the property of the aircraft, through the inheritance interface to obtain.

Back to the topic, the Python language can have no interface functionality, but it can inherit multiple. Is Python supposed to be implemented with multiple inheritance? Yes, it's not. That is, because syntactically, it is true through multiple inheritance. Say no, because its inheritance still abide by "is-a" relationship, from the meaning of still follow the principle of single inheritance. How does that make sense? Let's look at an example.

class Vehicle (object):     Pass class Planemixin (object):     def Fly (self):         Print ' I am Flying ' class airplane (Vehicle, planemixin):     Pass

As you can see, the airplane class above implements multiple inheritance, but the second class it inherits is named Planemixin, not plane, which does not affect functionality, but tells the person who later reads the code, which is a mixin class. So from the meaning of understanding, airplane is just a vehicle, not a plane. this mixin, which means mixed (mix-in), tells others that this class is added as a function to a subclass, not as a parent class, and it functions as an interface in Java.

Use the Mixin class to implement multiple inheritance with great care

    • First it must represent a function, not an item, as Runnable,callable in Java.
    • Second it must be a single responsibility, if there are multiple functions, then write multiple Mixin class
    • Then, it does not depend on the implementation of the subclass
    • Finally, subclasses can still work even if they do not inherit the Mixin class, which is missing a function. (such as the aircraft can still carry passengers, just can't fly ^_^)

In addition, Reactjs also has a mixin function, and the syntax is simple:

var planemixin = function () {  return  {    fly:function () {      console.log ('  I am Flying');    }   = React.createclass ({  mixins: [Planemixin ()],  render:function () {    return '  ' ;  }});

Python Learning Day13 Python object-oriented Learning 2: @property, multiple inheritance

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.