python学习疑惑

静态方法,类方法,实例方法区别

class A:
    flage = 1

    @staticmethod
    def printHellow():
        print 'hellow word'

    @classmethod
    def printflag(cls):
        cls.flage = 2
        print cls.flage

    def printPython(self):
        print 'python'


a = A()
print a.flage
a.printPython()
A.printHellow()
A.printflag()

result:
1
python
hellow word
2

1.调用方式不同
2.类方法默认有cls属性,可以修改本类的属性值,实例方法有self对象,可以使用,修改本地成员变量,而静态方法则没有

动态的创建类包含实例方法,类方法,静态方法
@staticmethod
def printHellow():
    print 'hellow word'


@classmethod
def printflag(cls):
    cls.flage = 2
    print cls.flage


def printSelfFlage(self):
    print self.flage


Foo = type('Foo', (),
           {'flage': 5, 'printHellow': printHellow, 'printSelfFlage': printSelfFlage, 'printflag': printflag})

Foo.printHellow()
Foo.printflag()
foo = Foo()
foo.printSelfFlage()

元类中的属性
__class__属性

age = 35
print age.__class__

reult:
        #判断对象是什么类型的


__metaclass__属性
class Foo(Bar):
    pass

如果写了上述代码则做得以下操作
1.Foo中有__metaclass__这个属性吗?如果是,python会通过__metaclass__创建一个名字为Foo对象
2.如果Python没有找到__metaclass__,它会继续在Bar(父类)中寻找__metaclass__属性,并尝试做上面的操作
3.如果Python在任何父类中都找不到__metaclass__,它就会在模块层次中去寻找__metaclass__,并尝试做同样的操作。
4.如果还是找不到__metaclass__,Python就会用内置的type来创建这个类对象。



自定义元类
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

    return type(future_class_name, future_class_parents, newAttr)


class Foo(object):
    bar = 'bip'
    __metaclass__ = upper_attr


foo = Foo()
print foo.BAR


result:
bip

__slots__属性
限制class实例能添加的属性
生成器
1.创建方式一
l=(x*2 for x in range(5))

2.创建方式二
def gen():
    i = 0
    while True:
        temp = yield i
        print temp
        i = i + 1


取值:
l = gen()
print l.next()
print l.next()
print l.next()

0
None
1
None
2
*******************************
l = gen()
print l.next()
print l.send('dsada')
print l.next()

0
dsada
1
None
2

闭包
def line_conf(a, b):
    def line(x):
        return a * x + b

    return line


line1 = line_conf(1, 1)
line2 = line_conf(2, 3)

print line1(5)
print line2(6)

result:
6
15
装饰器
# coding=utf8
from functools import *


def trace1(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        print '装饰代码args=%s,kwargs=%s' % (args, kwargs)
        return func(*args, **kwargs)

    return wrapper


@trace1
def add(x, y):
    return x + y


def trace2(*args1, **kwargs1):
    def funargs(func):
        @wraps(func)
        def wrapper(*agrs2, **kwargs2):
            print 'Hellow args1=%s,kwargs1=%s' % (args1, kwargs1)
            return func(*agrs2, **kwargs2)

        return wrapper

    return funargs


@trace2('ss')
def multi(x, y):
    return x * y


print add(3, 3)
print multi(3, 2)

result:
装饰代码args=(3, 3),kwargs={}
6
Hellow args1=('ss',),kwargs1={}
6

==,is的区别

1.is 是比较两个引用是否指向了同一个对象(引用比较)。
2.== 是比较两个对象是否相等

浅拷贝与深拷贝
import copy

orign = [1, 2, [3, 4]]
copy1 = copy.copy(orign)
copy2 = copy.deepcopy(orign)

print copy1
print copy2

orign[0] = 'ss'
print copy1
print copy2

orign[2][1] = 9

print copy1
print copy2

result:
[1, 2, [3, 4]]
[1, 2, [3, 4]]
[1, 2, [3, 4]]
[1, 2, [3, 4]]
[1, 2, [3, 9]]
[1, 2, [3, 4]]

copy.copy() 浅拷贝,只拷贝第一层
copy.deepcopy() 深拷贝,

你可能感兴趣的:(python学习疑惑)