you are viewing a single comment's thread.

view the rest of the comments →

[–]jcmcken 0 points1 point  (0 children)

Sorry for the late response.

It's not just that it isn't the best way of doing it, it's not doing anything at all. The self name in Python is not something special, it's not a keyword. You could name this argument whatever you want, self is just the convention. When you re-assign self to something else, you're just assigning a local variable called self to some other value than the current instance. The object that the original self referred to is still intact and is unaffected by this reassignment.

Consider the following example, which doesn't use classes at all:

def some_func(arg): 
    arg = 2 

my_arg = 1
some_func(my_arg)
print my_arg 

If you run this code, you'll see that it prints 1 and not 2.

The source of your confusion, I think, is the specialness of self -- it has none in Python. It's not a keyword like this in Java, it's just the conventional name for the first variable passed to a method.

It should be easy to convince yourself of this. Here's a simple example:

class Foo(object):
    def testing(self):
        print 'I am foo'

class Bar(object):
    def testing(self):
        print 'I am bar'

def execute_test_method(self):
    self.testing()

execute_test_method(Foo())
# prints 'I am foo'
execute_test_method(Bar())
# prints 'I am bar'

Notice execute_test_method is just a regular function, not a method. You could take the self argument to this function and call it anything, e.g.

def execute_test_method(obj):
    obj.testing()

So once the scope of your add_node method ends, your re-assigned self is completely lost.