python3.6 Object-oriented multi-inheritance and adorner

Source: Internet
Author: User
Tags instance method

# Multiple Inheritance

Class A:

Def show (self):

Print (' AAAA ')


Class B:

def fun (self):

Print (' BBBB ')


Class C (B,a):

Pass


x = C ()



# Special Methods for classes

‘‘‘

Class Properties:

__DICT__: Properties of the Class (contains a dictionary, consisting of the data properties of the Class)

__DOC__: Document string for Class

__NAME__: Class name

‘‘‘

x = ' A\NB '

#print (repr (x))

#实例调用:

‘‘‘

__INIT__ initialization

__REPR__ C1

__STR__ Print (c1) (if __repr__ is defined in the class, print x also returns the corresponding

__CALL__ C1 () enables an instance to be called

‘‘‘

Class Rectangle:

def __init__ (self,width,height):

Self.width = width

Self.height = height

def __str__ (self):

Return ' width is%s, height is%s '% (self.width,self.height)

def __repr__ (self):

Return ' area of%s '%self.area ()

def __call__ (self):

Return ' hahaha '

def area (self):

Return Self.width*self.height

def __add__ (Self,other):

If Isinstance (other,rectangle):

Return Self.area () +other.area ()

C1 = Rectangle (3,4)

C2 = Rectangle (5,6)

# operator Magic Method:

‘‘‘

__add__ (Self,other) x+y

__sub__ (self,other) x-y

__mul__ (Self,other) x*y

__mod__ (Self,other) x%y

__iadd__ (Self,other) x+=y

__isub__ (Self,other) x-=y

__radd__ (Self,other) y+x

__rsub__ (Self,other) y-x

__imul__ (Self,other) x*=y

__imod__ (Self,other) x%=y

‘‘‘

# Decorator

‘‘‘

@property The decorated function returned is no longer a function, but a Property object

The decorated method is no longer a callable object and can be viewed as a direct access to data properties.

@staticmethod decorate a function without parameters into a function that can be called by an instance.

There are no parameters when the function is defined.

@classmethod the decorated method into a Classmethod class object, which can be called by the class and can be called by the instance.

Note that the parameter is CLS represents the class itself. Instead, the instance method can only be called by the instance.

‘‘‘

Class Rectangle:

def __init__ (self,width,height):

Self.width = width

Self.height = height

@property

def area (self):

Return Self.width*self.height

@staticmethod

def fun ():

Return ' xxxxxx '

@classmethod

Def show (CLS):

Print (CLS)

Return ' YYYY '

C1 = Rectangle (3,4)

def fun1 (FF):

def fun2 (y):

return ff (y) +100

Return fun2


@fun1 #ff = fun1 (ff) # X=FF

def ff (y):

Return Y*y


def filterarg (x):

def fit (*arg):

If Len (arg) = = 0:

return 0

For I in ARG:

If not isinstance (i,int):

return 0

return x (*arg)

return fit


# @filterarg

def sums (*arg):

return sum (ARG)


Filterarg (sums) (3,4,5)


@filterarg

def average (*arg):

Print (ARG)

return sum (ARG)/len (ARG)





















This article is from the "11822904" blog, please be sure to keep this source http://11832904.blog.51cto.com/11822904/1910951

python3.6 Object-oriented multi-inheritance and adorner

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.