My Wiki!

Table of Contents

python programming

Python

Importing

[td@localhost zine]$ grep -ri ClosingIterator *
application.py:from zine.utils import ClosingIterator, local, local_manager, dump_json, \
application.py:        return ClosingIterator(self.dispatch_wsgi(environ, start_response),
Binary file application.pyc matches
Binary file utils/__init__.pyc matches
utils/__init__.py:from werkzeug import url_quote, Local, LocalManager, ClosingIterator
[td@localhost zine]$ 

application.py imports ClosingIterator from zine.utils. But zine.utils is a package. ClosingIterator is made avaialbe through the import in utils/init.py.

Class

self

36 down vote

I have been confused by this as well for quite a while and I don’t believe that the reason for this has got much to do with the often-pronounced explicit is better than implicit but that it is just following a simple analogy there.

Let’s take a simple vector class:

class Vector(object):

  def __init__(self, x, y):
      self.x = x
      self.y = y

Now, we want to have a method which calculates the length. What would it look like if we wanted to define it inside the class?

def length(self):
    return math.sqrt(self.x ** 2 + self.y ** 2)

And, what should it look like when we were to define it as a global method/function?

def length_global(vector):

  return math.sqrt(vector.x ** 2 + vector.y ** 2)

So, the whole structure stays the same. Now, how can me make use of this? If we assume for a moment that we hadn’t written a length method for our Vector class, we could do this:

Vector.lengthnew = lengthglobal v = Vector(3, 4) print v.length_new() # 5.0

This works, because the first parameter of lengthglobal, can be re-used as the self parameter in lengthnew. This would not be possible without an explicit self.

Another way of understanding the need for the explicit self is to see where Python adds some syntactical sugar. When you keep in mind, that basically, a call like

v_instance.length()

is internally transformed to

Vector.length(v_instance)

it is easy to see where the self fits in. You don’t not actually write instance methods in Python; what you write is class methods which (must) take an instance as a first parameter. And therefore, you’ll have to place the instance parameter somewhere explicitly.

Vector.lengthnew = lengthglobal… I actually started to use syntax like this in my class declarations. Whenever I only want to inherit some of the methods from another class, I just explicitly copy the reference to the methods. – Jeeyoung Kim Nov 22 '10 at 21:37

would it be fair to say that python's “instance method” is simply a syntactic sugar of static global methods (as in Java or C++) with an instance object passed in to package multiple attributes? — well this is kind of half-true since in polymorphism, the more important purpose of “this” (as in java) or “self” is to give u the correct implementation of methods. Python does have this. so calling myobj.someMethod() is equal to TheClassOfMyObj.someMethod(myobj) in python. note that the “TheClassOfMyObj” is automatically figured out by python from “self”, otherwise u'd have to find that out. – teddy teddy Sep 7 at 19:43


Navigation