zoukankan      html  css  js  c++  java
  • Python魔术师--self

                          (原文是 Python's Magical Self ,来自 http://concentricsky.com )

                 Python的self参数有时真让人抓狂,比如,你必须在每一个类的方法里显示定义self,然后,它会霸占不需要它们的地方。            

    class Foo(object):
        x = 9
        def __init__(self,x):
            self.x = x
     
        def bar(self,y):
            return self.x + y

                如果你有C++,Java或其他语言的编程背景,你会觉得 __init__ 和 bar 方法里的self 看起来很多余,python不是经常吹嘘自己的简答和优雅吗,self到底有什么用?

    作用域出现了

               在python里,作用域是非常简单的。Python里的一切都是对象,几乎任何东西都是在对象水平的作用域里。写一个模块试试?

    # test.py
    def say_hi():
        print 'Hi!'

              你刚刚创建了一个新的带有say_hi属性的模块对象。

              定义一个类?

    class Foo(object):
        x = 9
        def __init__(self,x):
            self.x = x
     
        def bar(self,y):
            return self.x + y

             你刚刚写了一个带有一些属性的类对象,那些属性是 x,__init__ ,还有 bar。

             实例化Foo?

    foo = Foo(5)

             你创建了一个带有属性x,__init__ ,和bar的Foo 实例,请记住,foo的三个属性跟Foo的不一样,待会,你就会知道为什么。

    上下文就是一切

             把Foo拆开:

    def bar(self,y):
        return self.x + y
     
    class Foo(object):
        x = 9
        def __init__(self,x):
            self.x = x
        bar = bar

             先不理bar的第一参数self,如果我们单单把bar看作普通的函数,那么,接下来的就很合理了。

    foo = Foo(5)
     
    print bar(foo,4) == 9
    print bar(Foo,0) == 9
             好像Foo.bar也可以这么做。

    print Foo.bar(foo,4) == 9
    print Foo.bar(Foo,0) == 9

            第一行打印出结果True,但第二行就出现了类型错误TypeError):未绑定的方法bar必须用Foo实例作为第一个参数(出现了不匹配的类型对象)。实例化一个Foo,并且修改bar,把self参数隐藏掉。

    print foo.bar(foo,4) == 9
    print foo.bar(foo,0) == 9
            两行代码都出现 类型错误(TypeError):bar() 需要两个参数(出现了3个)。为什么是2个,而不是3个?答案即将揭晓。

    绑定self

            如果你查一下三个bar的类型,你会发现,他们不全都一样。

    print type(bar)
    # <type 'function'>
    print type(Foo.bar)
    # <type 'instancemethod'>
    print type(foo.bar)
    # <type 'instancemethod'>
            把任何函数绑定到一个实例方法对象里,并把它封装在一个实例方法对象里,实例方法就会像胶水一样,粘着类、实例对象和原始的函数,最终它们都绑在一起。
    print Foo.bar.im_class == Foo
    print Foo.bar.im_func == bar
    print Foo.bar.im_self == None
    print foo.bar.im_class == Foo
    print foo.bar.im_func == bar
    print foo.bar.im_self == foo

            可以直截了当地用python写一个实例方法类。

    class myinstancemethod(object):
        def __init__(self,func,cls,instance=None):
            self.im_func = func
            self.im_class = cls
            self.im_self = instance
     
        def __call__(_self,*args,**kwargs):
            args = list(args)
            if _self.im_self is not None:
                args.insert(0,_self.im_self)
                 
            if len(args) == 0:
                raise TypeError("unbound method bar() must be called with Foo instance as first argument (got nothing instead)")
            elif not isinstance(args[0],_self.im_class):
                raise TypeError("unbound method bar() must be called with Foo instance as first argument (got %s instead)" % type(args[0]).__name__)
            else:
                return _self.im_func(*args,**kwargs)

              myinstancemethod 很正确地模仿了实例方法类,它跟前面的foo.bar 和Foo.bar的表现一样,除了它处理了一点类边缘情况和实例方法调用。

    my_unbound(self=foo,y=4)
    # TypeError: bar() got multiple values for keyword argument 'self'
    Foo.bar(self=foo,y=4)
    # TypeError: bar() got multiple values for keyword argument 'self'
     
    my_bound(self=foo,y=4)
    # TypeError: unbound method bar() must be called with Foo instance as first argument (got nothing instead)
    foo.bar(self=foo,y=4)
    # TypeError: unbound method bar() must be called with Foo instance as first argument (got nothing instead)
    

               这就是为什么你能够传入bar的引用,而不是传入foo,然后调用foo.bar。

    闭包

               foo 是一个与Foo完全不同的野兽。Python里的任一个变量都是内存里对象的引用——对象之间都没什么不同。Foo.x,Foo.__init__ 和 Foo.bar这三个与foo.x,foo.__Init__, 和foo.bar不同,他们都指向不同的内存空间。

    print Foo.x is not foo.x
    print Foo.__init__ is not foo.__init__
    print Foo.bar is not foo.bar

               Foo 和foo 是完全不相关的实体,它们只是碰巧在适当的时候相互引用对方。












      


  • 相关阅读:
    java并发编程:线程安全管理类--原子操作类--AtomicReferenceArray<E>
    java并发编程:线程安全管理类--原子操作类--AtomicReference<V>
    java并发编程:线程安全管理类--原子操作类--AtomicMarkableReference<V>
    java并发编程:线程安全管理类--原子操作类--AtomicLongFieldUpdater<T>
    java并发编程:线程安全管理类--原子操作类--AtomicLongArray
    RabbitMQ安装
    git push error: failed to push some refs to怎么解决
    GitLab的安装和启动、访问
    go mod模式下如何使用本地项目中的包
    推荐,慕课网好课,仿阿里系优酷网-企业级Go改造PHP项目踩坑避坑指北
  • 原文地址:https://www.cnblogs.com/Rex7/p/4752574.html
Copyright © 2011-2022 走看看