python advanced articles

python advanced articles

import import module

sys.path: Get the string collection of the specified module search path, you can put the written module under a certain path obtained, and you can find it correctly when importing in the program.

​ import sys

​ sys.path.append("")

Reimport the module

​ reload(module)

==、is
    a = [11,22,33]

    b = [11,22,33]

    >>>a == b

    True

    >>>a is b

    False

    >>>a = c

    >>>a is c

    True

Numbers in a certain range a is b, True, other ranges False

deep copy and shallow copy

​ a = [11,22,33]

​ b =a

​ >>>id(a)==id(b)

​ True

​ No data is copied, just copying the pointed position to it is a shallow copy

​ import copy

​ c = copy.deepcopy(a)

​ id(a) and id(c) are different, deep copy

​ a = [11,22,33]

​ b = [44,55,66]

​ c = [a,b]

​ d = copy.deepcopy(c)

​ e = copy.copy(c)

​ a.append(44)

​ >>>c[0]

​ [11,22,33,44]

​ >>>d[0]

​ [11,22,33]

​ >>>e[0]

​ [11,22,33,44]

After copy.copy() the list, the id is also different, but it will continue to copy

After changing to tuple, copy.copy(), same id, immutable tuple type, direct shallow copy

copy.copy() functions differently depending on mutable and immutable types

Convert between decimal, binary, octal, hexadecimal

privatization
  • xx: public variable

  • _x:from somemodule import * prohibits imports, class objects and subclasses can access

  • __xx: Double leading underscores to avoid naming conflicts with attributes in subclasses and cannot be directly accessed externally

  • ___xx___: Double underscores before and after, object or attribute

  • xx_: single trailing underscore to avoid conflicts with python keywords

###### 1. Add getter and setter methods to private properties

```python
class Money(object):

    def __init__(self):

        self.__money = 0

    def getMoney(self):

        return self.__money

    def setMoney(self,value):

        if  isinstance(value,int):

            self.__money = value

        else:

            print("error:不是整型数字")

```

###### 2. Use property to upgrade getter and setter methods

```python
class Money(object):

    def __init__(self):

        self.__money = 0

    def getMoney(self):

        return self.__money

    def setMoney(self,value):

        if isinstance(value,int):

            self.__money = value

        else:

            print("error:不是整型数字")

    money = property(getMoney,setMoney)

```

Note :

  1. Whether t.num calls getNum() or setNum() depends on the actual scene;

  2. If you assign a value to t.num, then you must call setNum()

  3. If you want to get the value of t.num, then you must call getNum()

  4. The role of property: It is equivalent to encapsulating the method, and it is more convenient for developers to set data on properties

    ```python
    class Money(object):

        def  __init__(self):
    
            self.__money = 0
    
        @property
    
        def money(self):
    
            return self.__money
    
        @money.setter
    
        def money(self,value):
    
            if  isinstance(value,int):
    
                self.__money = value
    
            else:
    
                print("error:不是整型数字")

    ```

    #### iterator

    1. Iterable objects

    The data types that act directly on the for loop are as follows:

    One is the collection data type, such as list, tuple, dict, set, str, etc.;

    One is generator, including generator and generator function with yield.

    These objects that can act directly on the for loop are collectively called iterable objects: iterable.

    ##### 2. Determine whether it is possible to iterate

    You can use isinstance() to determine whether an object is lterable:

    ```python
    from collections import Iterable

    isinstance("abc",Iterable)
    
    >>>True

    ```

    ##### 3. Iterator

    An object that can be called by the next() function and continuously returns the next value becomes an iterator: Iterator

    ​ from collections import Iterator

    ​ isinstance([],Iterator)

    ​ >>>False

    ​ isinstance((x for x in range(10)),Iterator)

    ​ >>>True

    ##### 4.iter() function

    Generators are Iterator objects, but although list, dict, and str are Iterables, they are not Iterators.

    To turn iterables such as list, dict, str, etc. into Iterators, you can use the iter() function:

    ​ isinstance(iter([]),Iterator)

    ​ >>>True

    #### Closure

    ```python #Define
    another function inside the function, and this function uses the variables of the outer function, then this function and some variables used are called closures

    def  test(number):
    
        print("----1------")
    
        def test_in(number2):
    
            print("------2----")
    
            print(number-number2)
    
        print("-----3-----")
    
        return test_in
    
    ret = test(100)
    
    ret(1)
    
    >>>----1------
    
        -----3-----
    
        ------2----
    
        99
    
    def  test(a,b):
    
        def  test_in(x):
    
            print(a*x+b)
    
        return  test_in
    
    line1 = test(1,1)   
    
    line1(0)
    
    line2 = test(10,4)
    
    line2(0)
    
    line1(0)

    ```

    #### decorators

    ​ def w1(func):

    ​ def inner():

    ​ print("----Verifying permissions------")

    ​ func()

    ​ return inner

    ​ def f1():

    ​ print("----f1-----")

    ​ def f2():

    ​ print("----f2-----")

    InnerFunc = w1 (f1)

    InnerFunc ()

    f1 assigns the address, f1() calls the function

    ​ def w1(func):

    ​ def inner():

    ​ print("----Verifying permissions------")

    ​ func()

    ​ return inner

    ​ def f1():

    ​ print("----f1-----")

    ​ def f2():

    ​ print("----f2-----")

    F1 = w1 (f1)

    F1 ()

    @w1 is equivalent to w1(f1)

    def w1(func):

    ​ def inner():

    ​ print("----Verifying permissions------")

    ​ func()

    ​ return inner

    ​ @w1

    ​ def f1():

    ​ print("----f1-----")

    ​ @w1

    ​ def f2():

    ​ print("----f2-----")

    F1 ()

    ​ f2()

    ###### Multiple decorators

    ​ def makeBold(fn):

    ​ def wrapped():

    ​ print("----1------")

    ​ return "" + fn() + ""

    ​ return wrapped

    Def makeItalic (fn):

    ​ def wrapped():

    ​ print("----2------")

    ​ return "" + fn() +""

    ​ return wrapped

    ​ @makeBold#As long as the python interpreter executes this code, it will be decorated automatically, instead of waiting for the call to be decorated

    @MakeItalic

    ​ def test3():

    ​ print("--------3-----")

    ​ return "hello world-3"

    Ret = test3 ()

    Print (ret)

    ​ >>>----1------

    ​ ----2------

    ​ --------3-----

    hello world-3

    Execute from top to bottom, decorate from bottom to top

    ​ def w1(func):

    ​ print("---- is decorating 1-----")

    ​ def inner():

    ​ print("------Verifying permission 1-----")

    ​ def w2(func):

    ​ print("---- is decorating 2-----")

    ​ def inner():

    ​ print("----Verifying permission 2----")

    ​ func()

    ​ return inner()

    ​ @w1

    ​ @w2

    ​ def f1():

    ​ print("----f1----")

    ​ f1() #Before calling f1, it has been decorated

    ​ >>>---- Decorating 2-----

    ​ ----Decorating 1-----

    ​ ------Verifying permission 1-----

    ​ ----Verifying permission 2----

    ---- f1 ----

    ###### Decorators decorate functions with parameters and without parameters

    ```python
    def func(functionName):

        print("----func---1---")
    
        def  fun_in(*args,**kwargs):#定义不定长参数
    
            print("---func_in---1---")
    
            functionName(*args,**kwargs)
    
            print("---func_in---2---")
    
        print("---func---2---")
    
        return func_in
    
    @func
    
    def  test1(a,b,c):
    
        print("----test-a=%d,b=%d,c=%d---"%(a,b,c))
    
    @func
    
    def  test2(a,b,c,d):
    
        print("----test-a=%d,b=%d,c=%d,d=%d---"%(a,b,c,d))
    
    test1(11,22,33)
    
    test2(44,55,66,77)
    
    >>>----func---1---
    
        ---func---2---
    
        ---func---1---
    
        ---func---2---
    
        ---func_in---1---
    
        ----test-a=11,b=22,c=33---
    
        ---func_in---2---
    
        ---func_in---1---
    
        ----test-a=44,b=55,c=66,d=77---
    
        ---func_in---2---

    ```

    ###### Decorators decorate parameters with return values

    ```python
    def func(functionName):

        print("---func---1---")
    
        def  func_in():
    
            print("---func_in---1---")
    
            ret = functionName()#保存返回来的haha
    
            print("---func_in---2---")
    
            return ret  #把haha返回到调用处
    
        print("---func---2---")
    
    @func
    
    def  test():
    
        print("----test----")
    
        return  "haha"
    
    ret = test()
    
    print("test return value is %s"%ret)

    decorator with parameters

    def  func_arg(arg):
    
        def  func(functionName):
    
            def  func_in():
    
                print("---记录日志-arg=%s--"%arg)
    
                if  arg =="heihei":
    
                    functionName()
    
                    functionName()
    
                else:
    
                    functionName()
    
            return func_in()
    
        return  func
    
    @func_arg("heihei")#带有参数的装饰器,能够起到在运行时,有不同的功能
    
    def  test():
    
        print("--test--")
    
    @func_arg("haha")
    
    def  test2():
    
        print("--test2--")
    
    test()
    
    test2()

    ```

    #### scope

    ###### LEGB Rules

    locals -> enclosing function -> globals -> builtings

    locals: the current namespace

    enclosing function: namespace for outer nested functions (common in closures)

    globals: global variables

    builtings: embedded

    ##### Dynamically add properties and methods

    Add property class/instance.property=XX

    The addition method cannot be as above,

    ​ import types.MethodType

    ​ Binding object. function name () = types. MethodType (function name, bound object)

    ###### __slots__

    For the purpose of restriction, Python allows a special __slots__ variable to be defined when defining a class to limit the attributes that a class instance can add:

    ```python

    class Person(object):
    slots = ("name","age")

    P = Person()
    P.name = "Pharaoh"
    P.age = 20 #Adding
    other attributes will report an
    error```

    ##### Builder

    Calculate while looping

    ###### Create generator method

    1. Change the [] of the list comprehension to (), and get the next return value of the generator through the next function

    python def creatNum(): print("-----start-----") a,b = 0,1 for i in range(5): print("----1----") yield b #程序停下来,把yield后面的值返回 print("----2----") a,b = b,a+b print("----3----") print("----stop----") >>>a = creatNum() >>>a <generator object creatNum at 0x7f42d27de7d8> >>>next(a) -----start----- ----1---- 1 >>>next(a)#等价于a.__next__() ----2---- ----3---- ----1---- 1

    python def test(): i = 0 while i<5: temp = yield i print(temp) i+=1 >>>t = test() >>>t.__next__() [out] 0 >>>t.__next__() None [out] 1 >>>t.__next__() None [out] 2 >>>t.send("haha") haha [out] 3

    ##### classes as decorators

    Define a __call__ method, the class can be called directly

class Test(object):
    def __init__(self,func):
        print("---初始化---")
        print("func name is %s"%func.__name__)
        self.__func = func
    def __call__(self):
        print("---装饰器中的功能---")
        self.__func()
@Test
def test():#当用Test来装作装饰器对test函数进行装饰的时候,首先会创建一个Test的实例对象,func指向test(),func.__name__函数名
    print("---test---")
test()
Metaclass

A class is also an object

Create a class using type

python = type(类名,由父类名称组成的元组(针对继承的情况可以为空),包含属性的字典) Person = type("Person",(),{"num":0}) p1 = Person() >>>p1.num 0 def printNum(self): print("---num-%d---"%self.num) >>>Test3 = type("Test3",(),{"printNum":printnum}) t1 = Test3() t1.num = 100 t1.printNum() ---num-100---

__metaclass__ attribute
def upper_attr(future_class_name,future_class_parents,future_class_attr):
    #遍历属性字典,把不是__开头的属性名字变为大写
    newAttr = {}
    for name,value in future_class_attr.items():
        if not name.startswith("__"):
            newAttr[name.upper()] = value
    #调用type来创建一个类
    return type(future_class_name,future_class_parents,newAttr)
class Foo(object,metaclass = upper_attr):
   #设置Foo类的元类为upper_attr
    bar = "bip"
print(hasattr(Foo,'bar'))
print(hasattr(Foo,'BAR'))
f = Foo()
print(f.BAR)
garbage collection

1. Small integer object pool

[-5,257) These integer objects are pre-established and will not be garbage collected. In a python program, all integers in this range use the same object

2. Large integer object pool

3.intern mechanism

  • Small integer shared object, resident in memory
  • Single character shared object, resident in memory
  • A single word, unmodifiable, the intern mechanism is enabled by default, shared objects, and the reference count is 0, it will be destroyed
Garbage collection (GC garbage collection)

Python adopts a strategy based on the reference counting mechanism, supplemented by two mechanisms of mark-sweep and generational collection.

Advantages of reference counting mechanism:

  • Simple
  • Practicality: Once there are no references, the memory is freed directly. The time to process the reclaimed memory is amortized to the usual time

shortcoming:

  • Maintaining reference counts consumes resources

  • circular reference

python list1 = [] list2 = [] list1.append(list2) list2.append(list1)

Referring to each other, the reference counts of list1 and list2 are still 1, and the occupied memory can never be reclaimed.

Linked list reference counting, generational collection

1. The case of reference count + 1

  • object is created, e.g. a=23
  • object is referenced, e.g. b = a
  • Objects are passed as parameters to a function, such as func(a)
  • The object is stored as an element in a container, e.g. list1=[a,a]

2. The case of reference count -1

  • The alias of the object is explicitly destroyed, e.g. del a
  • The alias of the object is given to the new object, e.g. a=24
  • An object leaves its scope, such as when the func function finishes executing, the local variables in the func function (the global variables will not)
  • The container in which the object resides is destroyed, or the object is removed from the container

3. View the reference count of an object

python import sys a = "hello world" sys.getrefcount(a) >>>2

python import gc#gc默认运行 gc.disable()#关闭gc gc.collect()#手动调用collect

##### Built-in properties

Common Proprietary Attributes illustrate touch method
__init__ Construct the initialization function After creating an instance, it is used when assigning, after __new__
__new__ Properties required to generate an instance When creating an instance
__class__ class of instance instance.__class__
__str__ instance string representation, readability print(class attribute), if not implemented, use repr result
__repr__ instance string representation, accuracy Class instance carriage return or print(repr(class instance))
__the__ destruct del delete instance
__dict__ instance custom properties vars(instance.__dict__)
__doc__ Class documentation, subclasses cannot inherit help(class or instance)
__getattribute__ property access interceptor When accessing instance properties
__base__ All parent class constituent elements of a class classname.__bases__

#### Built-in functions

###### map function

map(function,sequence[,sequence,...]) -> list

  • function: is a function
  • sequence: is one or more sequences, depending on how many parameters the function takes
  • The return value is a list

```python #The
function needs a parameter
map(lambda x:x*x,[1,2,3])
# The result is: [1,4,9]

#The function requires two parameters
map(lambda x, y: x+y,[1,2,3],[4,5,6])
#The result is: [5,7,9]

def f1(x, y):
return (x,y)
l1 =[0,1,2,3,4,5,6]
l2 =['Sun','M','T','W','T','F','S']
l3 =map(f1,l1,l2)
print(list(l3))
#结果为:[(0,'Sun'),(1,'M'),(2,'T'),(3,'W'),(4,'T'),(5,'F'),(6,'S')]

```

##### filter function

perform a filter on the specified object

The filter function will call the function function on each element in the sequence parameter sequence, and the final returned result contains the element whose calling result is True.

The type of the return value is the same as the type of the parameter sequence

python filter(lambda x: x%2,[1,2,3,4]) [1,3] filter(None,"she") 'she'

##### reduce function

reduce takes an element from the sequence in turn, and calls the function again with the result of the last call to the function as a parameter. When calling the function for the first time, if the initial parameter is provided, the function will be called with the first element in the sequence and initial as parameters, otherwise the function will be called with the first two elements in the sequence sequence as parameters. Note that the function function cannot be None .

```python
reduce(lambda x, y: x+y,[1,2,3,4])
10

reduce(lambda x,y:x+y,[1,2,3,4],5)
15

reduce(lambda x, y:x+y,['aa','bb','cc'],'dd')
'ddaabbcc' The
reduce function in python3 should be introduced first: from functools import reduce
```

##### sorted function

```python
a = [55,22,77,99]
a.sort()

a
[22,55,77,99]
a.sort(reverse=Ture)
a
[99,77,55,22]

sorted([1,5,4,2])

[1,2,4,5]
sorted([1,5,4,2],reverse=1)
[5,4,2,1]
```

#### collection set

```python
a=[11,55,44,22,11,11]
b = set(a)

b
{11,55,44,22}
a=list(b)
a
[11,55,44,22]

a = "abcdef"
b = set(a)

b
{'a','b','c','d','e','f'}
A="bdfhuy"
B = set(A)
B
{'b','d','f','h','u','y'}
b&B
{'b','d','f'}
b|B
{'a','b','c','d','e','f,'h','u','y'}
b-B
{'a','c','e'}
b^B
{'a','c','e','h','u','y'}
```

##### functools

###### partial functions

```python
import functools
def showarg(*args,**kw):
print(arg)
print(kw)

p1=functools.partial(showarg,1,2,3)
p1()
p1(4,5,6)
p1(a='python',b='itcast')

(1,2,3)
{}
(1,2,3,4,5,6)
{}
(1,2,3)
{'a':'python','b':'itcast'}
```

###### wraps function

When using a decorator, the decorated function is actually another function (function attributes such as function names will change, and a decorator called warps is provided in the functools package to eliminate such side effects.

import functools
def note(func):
    "note function"
    @functools.wraps(func)
    def wrapper():
        "wrapper function"
        print('note something')
        return func()
    return wrapper

@note
def test():
    "test function"
    print('I am test')
    
test()
print(test.__doc__)

module

hashlib encryption algorithm

import hashlib
m = hashlib.md5()#创建hash对象,md5
print m
m.update('itcast')#更新哈希对象以字符串参数
print(m.hexdigest())#返回十六进制数字字符串

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325342479&siteId=291194637