Python, __init__ and native confusion

Ok, so I looked at some source when I came across this:

>>> def __parse(self, filename): ... "parse ID3v1.0 tags from MP3 file" ... self.clear() ... try: ... fsock = open(filename, "rb", 0) ... try: ... fsock.seek(-128, 2) ... tagdata = fsock.read(128) ... finally: ... fsock.close() ... if tagdata[:3] == 'TAG': ... for tag, (start, end, parseFunc) in self.tagDataMap.items(): ... self[tag] = parseFunc(tagdata[start:end]) ... except IOError: ... pass ... 

So, I decided to check it out.

  >>> __parse("blah.mp3") 

And I got this error:

 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: __parse() takes exactly 2 arguments (1 given) 

This is not the first time I have come across this, I always think that I should include self in the argument parameter list, but I know that it is not. Can someone explain to me why this happens so often with the code I'm trying to play with, I believe that because of my level of understanding of the terms, I don’t even understand what init or self is, or why it matters. def x (b): print b is the same as def x (self, b): self.b = b print self.b, right? Why is this so important!

I just want a basic explanation, so I can get it out of my head, thanks.

+3
source share
5 answers

def __parse was inside a specific class definition.

You cannot pull the defs method from class definitions. The definition of a function of a method is part of the class.

Take a 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.

  • The function does not use self .

  • The class method uses self . Generally, all instance methods will use self , unless they have special decorators like @classmethod that say otherwise.

  • The function is independent of anything else.

  • The class method depends on the call of the Adder class instance; In addition, it depends on whether this instance of the Adder class has been initialized. In this case, the initialization function ( __init__ ) ensures that every instance of Adder always has an instance variable called grand_total , and this instance variable has an initial value of 0 .

  • You cannot pull out the add method function from the Adder class and use it separately. This is not a standalone feature. It has been defined inside the class and has certain expectations due to this location inside the class.

+8
source

Functions / methods can be written outside the class and then used for a method 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() 
+2
source

It sounds like you are a bit confused about classes and object-oriented programming. "I" is one of the problems in python for people coming from other programming languages. IMO's official textbook doesn't do it too well. This tutorial seems pretty good.

If you've ever studied 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 for each function in the class definition.

If python is your first programming language (or your first object-oriented language), you can remember this as a simple rule: if you define a function that is part of a class, you need to include self as the first argument. If you define a function that is not part of the class, you should not include self in the arguments. You cannot take a class function and make it autonomous without doing some (or possibly large) extra coding. Finally, never include self as an argument when calling a function.

There are exceptions to these rules, but now you should not worry.

+1
source

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

0
source

As an aside, you can create static class methods in Python. The easiest way to do this is through decorators (e.g. @staticmethod ). I suspect that this kind of thing is usually not a pythonic solution.

0
source

All Articles