Python, __init__ and self confusion

拜拜、爱过 提交于 2019-12-01 20:49:23

The def __parse was inside some class definition.

You can't pull the method defs out of the class definitions. The method function definition is part of the class.

Look at these two examples:

def add( a, b ):
    return a + b

And

class Adder( object ):
    def __init__( self ):
        self.grand_total = 0
    def add( self, a, b ):
        self.grand_total += a+b
        return a+b

Notes.

  1. The function does not use self.

  2. The class method does use self. Generally, all instance methods will use self, unless they have specific decorators like @classmethod that say otherwise.

  3. The function doesn't depend on anything else else.

  4. The class method depends on being called by an instance of the class Adder; further, it depends on that instance of the class Adder having been initialized correctly. In this case, the initialization function (__init__) has assured that each instance of Adder always has an instance variable named grand_total and that instance variable has an initial value of 0.

  5. You can't pull the add method function out of the Adder class and use it separately. It is not a stand-alone function. It was defined inside the class and has certain expectations because of that location inside the class.

Functions/methods can be written outside of a class and then used for a technique in Python called monkeypatching:

class C(object):
  def __init__(self):
    self.foo = 'bar'

def __output(self):
  print self.foo

C.output = __output
c = C()
c.output()

Looks like you're a bit confused about classes and object-oriented programming. The 'self' thing is one of the gotchas in python for people coming from other programming languages. IMO the official tutorial doesn't handle it too well. This tutorial seems quite good.

If you've ever learnt java, self in python is very similar to this in java. The difference is that python requires you to list self as the first argument to every function in a class definition.

If python is your first programming language (or your first object-oriented language), you could remember this as a simple rule-of-thumb: if you're defining a function that's part of a class, you need to include self as the first argument. If you're defining a function that's not part of a class, you shouldn't include self in the arguments. You can't take a class function and make it stand-alone without doing some (or possibly a lot of) extra coding. Finally, never include self as an argument when calling a function.

There are exceptions to those rules, but they're not worth worrying about now.

self is passed in automatically by the instancemethod wrapper on classes. This function isn't wrapped; it's not a method, it's just a function. It doesn't even make sense without being attached to a class, since it needs the self parameter.

As an aside, it is possible to create static methods of a class in Python. The simplest way to do this is via decorators (e.g. @staticmethod). I suspect this kind of thing is usually not the Pythonic solution though.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!