[Tutor] self parameter
Benoit Dupire
bdupire@seatech.fau.edu
Thu, 10 May 2001 00:00:23 -0400
Quite.
time.localtime() returns a tuple, so yes you are unpacking the tuple.
But there is no 'real' function in this code.
You are defining a class.
>From this class you will be able to instanciate objects.
Objects have a set of operations you can call, which are called methods.
__init__ is therefore a method.
It's like a function, but this function can only operate on objects of
the same class.
When you define __init__ you don't know on which object ( in other words
on which instance of the class now) it will operate.
The __init__ method is said to be unbound.
To let it know on which object it gonna operate, we pass to it a
parameter, which is the object itself (to be accurate, a reference to
this object).
So when you are doing something like : self.foo = 5
then you create a new instance variable (a variable within your object)
which is called foo.
Python knows in which object it has to create foo, because self is a
reference to this object.
I know, there is quite a bit of OO terminology here... :o)
Hope this helps....
Benoit
Katharine Stoner wrote:
> I am trying to understand how special methods work.Ex:class now:
> def _init_(self):
> self.t = time.time()
> self.year, \
> self.month, \
> self.day, \
> self.hour, \
> self.minute, \
> self.second, \
> self.dow, \
> self.doy, \
> self.dst = time.localtime(self.t)I don't know if I got it right,
> but I think the function is passing data to each variable, t; year;
> month; etc. It's unpacking the tuple. I'm not sure I understand how
> this bit of code is working. The self arguement is allowing the
> function to pass data right?I'd much appreciate any
> assistance.-Cameron
--
Benoit Dupire
Graduate Student