Using super with a class method

后端 未结 5 522
囚心锁ツ
囚心锁ツ 2020-12-13 05:48

I\'m trying to learn the super() function in Python.

I thought I had a grasp of it until I came over this example (2.6) and found myself stuck.

http://www.ca

5条回答
  •  情话喂你
    2020-12-13 06:17

    I think I've understood the point now thanks to this beatiful site and lovely community.

    If you don't mind please correct me if I'm wrong on classmethods (which I am now trying to understand fully):

    
    # EXAMPLE #1
    >>> class A(object):
    ...     def foo(cls):
    ...             print cls
    ...     foo = classmethod(foo)
    ... 
    >>> a = A()
    >>> a.foo()
    # THIS IS THE CLASS ITSELF (__class__)
    class '__main__.A'
    
    # EXAMPLE #2
    # SAME AS ABOVE (With new @decorator)
    >>> class A(object):
    ...     @classmethod
    ...     def foo(cls):
    ...             print cls
    ... 
    >>> a = A()
    >>> a.foo()
    class '__main__.A'
    
    # EXAMPLE #3
    >>> class B(object):
    ...     def foo(self):
    ...             print self
    ... 
    >>> b = B()
    >>> b.foo()
    # THIS IS THE INSTANCE WITH ADDRESS (self)
    __main__.B object at 0xb747a8ec
    >>>
    

    I hope this illustration shows ..

提交回复
热议问题