[Tutor] self parameter

Walter Vannini walterv@jps.net
Thu, 10 May 2001 00:47:49 -0700


Here's one approach to understanding what's going on.
Read documentation, look at examples, and then try rewriting the
problem code in ways that SHOULD be equivalent.
Here's my attempt at a rewrite of the original code:

class now:
    def __init__(self):
        localfloat = time.time() 
        # localfloat references the float returned by time.time()
        localtuple = time.localtime(localfloat) 
        # localtuple references a 9 element tuple
        # returned by time.localtime
        self.t = localfloat 
        # create an attribute t that references the
        # same float referenced by localfloat
        (self.year, self.month, self.day, self.hour, self.minute, \
        self.second, self.dow, self.doy, self.dst) \
            = localtuple 
        # 9 new attributes are created, and assigned values
        # via the 9 element tuple returned by time.localtime

By simply getting rid of the local variables
localfloat and localtuple, and rewriting the very explicit
"(a,b) = c" assignment of self.year, self.month, etc
as "a,b = c", you get the original method.
Also, "_init_" should probably be changed to "__init__".

Hope that helps,

Walter.
	
> 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