python basics 10- Description and decorators

Answers before class

How 1.vim withdraw all know it, the configuration of pep8, so that there will quit when the error, and then quit once the ok q: quit

w: save

wq save and exit

q! : Force Quit

shift + zz: save and exit

x: save and exit

2. I recommend two software: Sunflower (remote control software), NetEase Youdao dictionary, Python official documents

3. Do not capitalized the first letter of the function name, the class name is capitalized, this is something convention (norm) function with the class name when

class Apple: # Apple class pass

4. recursive, first the first point, we much computational cost, slower second point, not properly smoothed again (a recursion depth)

5. do not understand when to go their own path again in accordance with the order of execution of the code, do not go into a dead end.

6. When I'm speaking of course, we do not try to follow me knock, first thinking reason clearly, listen to understand the other's revisit

7. The foundation of our school now, a lot of things are fixed, def, paradigm

1.new method singleton

--new - () method to achieve single-mode embodiment, only one instance of the object. Repeated instances of id is the same.

Examples of when the first call --new - () method, and then perform --init - () methods.

Earth class (Object): # python2 write object, old-style class

--new DEF - (CLS, * args, ** kwargs): #cls on behalf of the class itself.

if not hasattr(cls,'instance'):

Super = cls.instance () .-- new - (CLS) # The new parent class instance attributes to inherit

return cls.instance

--init DEF - (Self): #self instance itself.

self.name = 'earth'

Ordinary class instantiation time will open up a new memory space.

2. Access custom properties

Everything in python objects.

check:

hasattr (re, 'length') return bool value, you are determined that there is no attribute.

getattr (re.'length ') returns the property value

b .-- getattribute - ( 'length') # returns the property value

increase:

b.aaa =1

setattr(b,'bbb',2)

b.--setattr--('ccc',3)

change:

setattr(b,'length',6)

b .-- setattr - ( 'length', 5) underlying cube call this method.

delete:

delattr(b,'ccc')

b .-- delattr - ( 'bbb') underlying cube call this method.

the b

 

def --getattr--(self,item):

print ( 'no attribute') # magic rewriting method, when the attribute does not exist, if this method is defined, the method is called. changes being given to print this sentence.

 

3. descriptor

If an instance of another class in a class, this property is accessed when how to do?

class MyAttribute:

def --get--(self,instance,owner):

print('get')

def --set--(self,instance,value):

print('set')

def --delete--(self,instance):

print ( 'from')

class MyClass:

m = MyAttribute() #在另一个类里面实例化了外面的类. m 就是描述符

def --del--(self):

print('instance delete')

c = MyClass()

c.m #打印出了MyAttribute的--get--()方法里面的内容.

c.m = 1 #打印出了MyAttribute的--set--()方法里面的内容.

del c.m #打印出了MyAttribute的--delete--()方法里面的内容.

在类里面定义另一个类,对这个实例做访问时,需要定义 --get-- --set-- --delete--方法.

4.装饰器

闭包:函数里面嵌套函数,返回内层函数的函数体.

装饰器的作用:给原有的函数添加新的功能.在不修改以前的代码的前提之下去添加.

def func1(func):

  def func2(y):

    print('func2 is running')

    return func(y) + 1

  return func2

def gun(m):

  print('gun is running')

  return m * m

@func1

def deco(m):

  print('deco is running')

  return m * m

print(deco(2))
在原有的基础上加了1

 

内置装饰器 @property #原本要加括号调用.就像访问属性一样

示例:

class Rectangle:

def --init--(self,length,width):

self.length = length

self.width = width

def area(self):

areas = self.length * self.width

return areas

a = Rectangle(3,4)

print(a.area()) 变为 print(a.area),加property变的直接可以调用,不加括号.像访问属性一样.

@staticmethod #静态方法,没法访问类里面的类属性跟类参数方法了,但是可以不加self.相当于变成函数的效果.

类里面定义一个方法:

def func(): .

print(self.width)

@classmethod #类方法

def func1(cls):

print(cls.name)

原本是 类名.name. 加了类方法直接用cls.name代替.

 

类也可以当作装饰器.但需要定义call方法

class TestClass:

def --init--(self,func):

self.func = func

def --call--(self, *args , **kwargs):

print('--正在添加功能--')

return self.func()

@TestClass

def fun():

print('正在运行程序')

func()

datetime 模块

Guess you like

Origin www.cnblogs.com/winfun/p/10983838.html