Python learns Day 9 property multi-inheritance Mixin, pythonmixin

Source: Internet
Author: User

Python learns Day 9 property multi-inheritance Mixin, pythonmixin

When binding an attribute, If we expose the attribute directly, although it is easy to write, but we cannot check the parameter, so we can change the score as needed:

s = Student()

s.score = 9999

To limit the score range, you can use the set_score () method to set the score and get_score () to obtain the score. In this way, in the set_score () method, you can check the parameters:

class Student(object):

 

def get_score(self):

return self._score

 

def set_score(self, value):

if not isinstance(value, int):

raise ValueError('score must be an integer!')

if value < 0 or value > 100:

raise ValueError('score must between 0 ~ 100!')

self._score = value

>>> s = Student()

>>> s.set_score(60) # ok!

>>> s.get_score()

60

>>> s.set_score(9999)

Traceback (most recent call last):

...

ValueError: score must between 0 ~ 100!

The @ property modifier built in Python is responsible for calling a method into a property:

class Student(object):

 

@property

def score(self):

return self._score

 

@score.setter

def score(self, value):

if not isinstance(value, int):

raise ValueError('score must be an integer!')

if value < 0 or value > 100:

raise ValueError('score must between 0 ~ 100!')

self._score = value

@ Property implementation is complicated. Let's first look at how to use it. To convert a getter method to an attribute, you only need to add @ property. At this time, @ property itself creates another modifier @ score. setter is responsible for changing a setter method to attribute assignment, so we have a controllable attribute operation:

>>> S = Student ()

>>> S. score = 60 # OK, which is actually converted to s. set_score (60)

>>> S. score # OK, which is actually converted to s. get_score ()

60

>>> S. score = 9999

Traceback (most recent call last ):

...

ValueError: score must between 0 ~ 100!

Define the read-only attribute. Only the getter method is defined. The setter method is a read-only attribute:

class Student(object):

 

@property

def birth(self):

return self._birth

 

@birth.setter

def birth(self, value):

self._birth = value

 

@property

def age(self):

return 2014 - self._birth

Multi-Inheritance

Inheritance is an important way of object-oriented programming, because through inheritance, sub-classes can expand the features of the parent class.

First, the main category levels are still designed for mammals and birds:

Class Animal (object ):

Pass

 

# Category:

Class Mammal (Animal ):

Pass

 

Class Bird (Animal ):

Pass

 

# Various animals:

Class Dog (Mammal ):

Pass

 

Class Bat (Mammal ):

Pass

 

Class Parrot (Bird ):

Pass

 

Class Ostrich (Bird ):

Pass

To add Runnable and Flyable functions to animals, you only need to define the Runnable and Flyable classes first:

class Runnable(object):

def run(self):

print('Running...')

 

class Flyable(object):

def fly(self):

print('Flying...')

For an animal that requires the Runnable function, it inherits one more Runnable, for example, Dog:

class Dog(Mammal, Runnable):

pass

For animals that require the Flyable function, one more Flyable will be inherited, for example, Bat:

 

class Bat(Mammal, Flyable):

pass

Through multi-inheritance, a subclass can obtain all the functions of multiple parent classes at the same time.

 

Mixin

When designing the inheritance relationship of a class, the main line is generally inherited from a single one. For example, Ostrich inherits from Bird. However, if you need to "mix" additional functions, you can achieve it through multi-inheritance. For example, Let Ostrich inherit from Bird and Runnable at the same time. This design is usually called Mixin.

 

To better understand the inheritance relationship, change Runnable and Flyable to RunnableMixin and FlyableMixin. Similarly, you can define CarnivorousMixin and HerbivoresMixin, a meat animal, so that an animal can have several mixins at the same time:

 

class Dog(Mammal, RunnableMixin, CarnivorousMixin):

pass

The purpose of Mixin is to add multiple functions to a class. In this way, when designing a class, we give priority to combining multiple Mixin functions through multi-inheritance, instead of Designing Multi-Level complex inheritance relationships.

 

Many built-in Python libraries also use Mixin. For example, Python comes with two types of network services: TCPServer and UDPServer. To serve multiple users at the same time, you must use a multi-process or multi-thread model, these two models are provided by ForkingMixin and ThreadingMixin. Through combination, we can create suitable services.

 

For example, to write a multi-process TCP Service, the definition is as follows:

 

class MyTCPServer(TCPServer, ForkingMixin):

pass

Write a multi-threaded UDP Service, which is defined as follows:

 

class MyUDPServer(UDPServer, ThreadingMixin):

pass

If you want to develop a more advanced coroutine model, you can compile a CoroutineMixin:

 

class MyTCPServer(TCPServer, CoroutineMixin):

pass

Try

In this way, we don't need a complicated and huge inheritance chain. We can quickly construct the required subclass by combining different class functions.

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.