Reputation: 13908
I am a newcomer to Python. I don't understand how/why the self
argument is used:
class Person:
def __init__(self, name):
self.name = name
def sayHi(self):
print 'Hello, my name is', self.name
p = Person('Swaroop')
p.sayHi()
This code is from a Byte of Python. This code could have just as easily been written as:
def sayHi(self, name):
print 'Hello, my name is', name
p = Person('Swaroop')
p.sayHi()
...right? What is the purpose of making name
a field of self
?
Upvotes: 2
Views: 2771
Reputation: 63737
It seems you are oversimplifying a not so simple thing. In object-oriented programming, a Class is a Declarative Construct which gives a blueprint as to what an object (a manifestation of this blueprint) would contain (properties) and how it would behave (members). Every manifestation of such a Class is called an Object which has a defined and specific instance. From an object via any of this behavioral attributes called member functions/methods we need somehow to refer to that instance of the object and remember individual elements within it and make a clear distinction with the other non member entities.
Consider Your example
class Person:
def __init__(self, name):
self.name = name
def sayHi(self):
print 'Hello, my name is', self.name
Every Instance of this Person (Tom, Dick, Harry) is unique and within each instance, to refer back to itself we need some idiom like (self in Python, this ptr is C++ or this in Java).
So in the __init__
method when you need to demarcate between the name
attribute of Person with the name
parameter we can easily do so with self
. Not only that, at any instance we can keep on referring back to this name via the self
.
Making an instance of Person p=Person('Swaroop')
and then invoking sayHi contrasting to calling just a function sayHi which is not a part of an object has two implications
sayHi
of the instance of Person named Swaroop on the other hand would mean something like a physically existing Swaroop greeting back his Name who has a persistent memory and would never forget unless he adopts a new name. If you have a background of C++ and might be wondering why on earth do we need to add that extra parameter to the function call where as in C++ this pointer is never passed.
Well Candidly speaking it does. If you read the C++ calling convention, whether X86 or X64, the this pointer is passed through the register ecx
to the method to give an handle to itself. This is more explicit here where we deliberately pass the handle to the current instance to the method.
Upvotes: 6
Reputation: 1469
You can't compare a function in a class to just a function. Sure you can pass in name, but the idea of using a class is that you are encapsulating data. Having vars linked to self means you can pass around an object, and that object can always have access to its 'member variables'. Some other class can use an instance of Person and not need to know that persons name in order to say hi.
Upvotes: 0
Reputation: 92569
Actually no. Your second example is not correct. The 'self' keyword is a special reference that is passed to instance methods of a class. It represents the actual instance of the class at that scope. When you make a class it is just a general definition. Then you create instances of that class. Each instance is unique and thus the methods have to be able to work on that instance. You have no way of knowing them until they are made so 'self' passes them in for you.
For instance, if you created two instances of your person class:
p1 = Person("john")
p2 = Person("sam")
self.name value is different for each of those. And when you call sayHi() on each instance:
p1.sayHi()
> Hello, my name is john
p2. sayHi()
> Hello, my name is sam
You would not use the self keyword for unbound functions. That is, just general functions like what you have written as your second example. You would instead have to write it like this;
def sayHi(name):
print 'Hello, my name is', name
p = Person('Swaroop')
p.sayHi()
> sayHi("john")
But that would basically just pass a name string to a Person class and call its method. The idea here is that you create unique instances of Person with their own properties.
Upvotes: 2
Reputation: 27224
The purpose is to store the reference to the name so you can reference it in another method of the class e.g. def sayHi (self)
Upvotes: 0
Reputation: 212885
Have a look at the new method sayHiTo
:
class Person:
def __init__(self, name):
self.name = name
def sayHi(self):
print 'Hello, my name is', self.name
def sayHiTo(self, other):
print 'Hello', other.name, ' my name is', self.name
p1 = Person('Swaroop')
p2 = Person('codeninja')
p1.sayHiTo(p2)
p2.sayHiTo(p1)
Upvotes: 4