[Tutor] Re: q about method(self)

Stephen Harris cyberdiction@hotmail.com
Sat, 31 Aug 2002 16:09:21 -0700


This is a multi-part message in MIME format.

------=_NextPart_000_0021_01C25108.C47FE720
Content-Type: text/plain;
	charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable


"Emile van Sebille" <emile@fenx.com> wrote in message =
news:akqqpg$obt$1@main.gmane.org...
> Erik:
> > A class method generally takes "self" as its first argument.
>=20
> It is named self by convention only.  You can call it 'this' if you =
want.
>=20
> > I had
> > been wondering for some time why this is, since it seems redundant =
if
> > it's there for all class methods.  But I just realized -- you can =
call
> > an object's method without using dot notation in Python, like this:
> >
> > method(instance_reference, args)
>=20
> This might also be seen as function(inst_ref, args) which serves to =
show
> that a class method is not really much different from any function.  =
In this
> case, the instance being passed could come from any class.
>=20
> >
> > rather than only like this:
> >
> > instance_reference.method(args)
> >
> > I didn't realize that until just now.  But what I'm wondering is,
> > why/when would you want to use this form?

Since I was just reading about this in the Sams Teach yourself
Python in 24 Hours, which is introductory, obviously, I post:

"Although you can call a function or method and use the return value to =
assign to or create a variable, the function itself cannot create =
something outside its box=97unless the function uses the self parameter. =
We see how that's done in Listing 10.1. The way to create variables =
(members in OOP talk) in an object is the same way Python creates all =
variables=97by assigning a value to them. That's what is going on =
Listing 10.1; self.t, self.year, self.month, and so on are variables =
being created as members of class now. The localtime() function returns =
a 9-tuple; an assignment such as that shown in the previous listing =
unpacks the tuple, placing the values into the variables on the left =
side of the =3D sign, exactly the way you would expect. Dow stands for =
''day of the week," doy indicates "day number of the year," and dst is 1 =
if daylight savings time is being observed, 0 if not. =20
=20
 After you create an instance of class now, you can, outside the box, =
look inside the instance and see that the __init__() method has created =
variables. You can inspect the variables, you can modify them, you can =
add new methods, and you can call any methods that are inside the class; =
you can even modify the methods and variables after creating an =
instance. However, if you do that, when you create another instance, =
your added methods and variables won't show up in the new instance. =
(There is a way to do this, but it's not something you'll be wanting to =
do for a long time, and it won't be covered in this book.) =20
=20
 As you saw in the original listing for class now, which was presented =
in the preceding chapter (see Listing 9.2), creating an instance of a =
class is easy: =20

 n =3D now()
print "The year is", n.year =20
=20
You may notice that creating an instance of a class looks very much like =
calling a function. That's because it is; Python does a few magic tricks =
behind the scenes, by fabricating a generic object and then passing that =
as the self argument to a function call that effectively looks like =20
=20
 n =3D now.__init__(object) =20
=20
 After you've created an instance of a class, you might think that you =
could call the class's __init__() method directly, like this: =20
=20
 n =3D now()
o =3D now.__init__(n)
print "year", o.year, n is o =20
=20
 If you run the preceding code, you'll see that you get an =
AttributeError that tells you that the object o is not an instance of =
class now, but a None object. That's because the self argument you =
provided to __init__() was not really a reference to itself. Only Python =
can construct a generic object that will really work in such a call. =
Chalk it up to magic."

SH: I'm not sure what that last paragraph means.

Regards,
Stephen=20



------=_NextPart_000_0021_01C25108.C47FE720
Content-Type: text/html;
	charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dwindows-1252">
<META content=3D"MSHTML 5.50.4134.600" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>
<DIV>&nbsp;</DIV>
<DIV>"Emile van Sebille" &lt;<A=20
href=3D"mailto:emile@fenx.com">emile@fenx.com</A>&gt; wrote in message =
<A=20
href=3D"news:akqqpg$obt$1@main.gmane.org">news:akqqpg$obt$1@main.gmane.or=
g</A>...</DIV>
<DIV>&gt; Erik:<BR>&gt; &gt; A class method generally takes "self" as =
its first=20
argument.<BR>&gt; <BR>&gt; It is named self by convention only.&nbsp; =
You can=20
call it 'this' if you want.<BR>&gt; <BR>&gt; &gt; I had<BR>&gt; &gt; =
been=20
wondering for some time why this is, since it seems redundant if<BR>&gt; =
&gt;=20
it's there for all class methods.&nbsp; But I just realized -- you can=20
call<BR>&gt; &gt; an object's method without using dot notation in =
Python, like=20
this:<BR>&gt; &gt;<BR>&gt; &gt; method(instance_reference, args)<BR>&gt; =

<BR>&gt; This might also be seen as function(inst_ref, args) which =
serves to=20
show<BR>&gt; that a class method is not really much different from any=20
function.&nbsp; In this<BR>&gt; case, the instance being passed could =
come from=20
any class.<BR>&gt; <BR>&gt; &gt;<BR>&gt; &gt; rather than only like=20
this:<BR>&gt; &gt;<BR>&gt; &gt; instance_reference.method(args)<BR>&gt;=20
&gt;<BR>&gt; &gt; I didn't realize that until just now.&nbsp; But what =
I'm=20
wondering is,<BR>&gt; &gt; why/when would you want to use this =
form?</DIV>
<DIV>&nbsp;</DIV>
<DIV>Since I was just reading about this in the Sams Teach =
yourself</DIV>
<DIV>Python in 24 Hours, which is introductory, obviously, I post:</DIV>
<DIV>&nbsp;</DIV>
<DIV>"Although you can call a function or method and use the return =
value to=20
assign to or create a variable, the function itself cannot create =
something=20
outside its box=97unless the function uses the self parameter. We see =
how that's=20
done in Listing 10.1. The way to create variables (members in OOP talk) =
in an=20
object is the same way Python creates all variables=97by assigning a =
value to=20
them. That's what is going on Listing 10.1; self.t, self.year, =
self.month, and=20
so on are variables being created as members of class now. The =
localtime()=20
function returns a 9-tuple; an assignment such as that shown in the =
previous=20
listing unpacks the tuple, placing the values into the variables on the =
left=20
side of the =3D sign, exactly the way you would expect. Dow stands for =
''day of=20
the week," doy indicates "day number of the year," and dst is 1 if =
daylight=20
savings time is being observed, 0 if =
not.&nbsp;&nbsp;<BR>&nbsp;<BR>&nbsp;After=20
you create an instance of class now, you can, outside the box, look =
inside the=20
instance and see that the __init__() method has created variables. You =
can=20
inspect the variables, you can modify them, you can add new methods, and =
you can=20
call any methods that are inside the class; you can even modify the =
methods and=20
variables after creating an instance. However, if you do that, when you =
create=20
another instance, your added methods and variables won't show up in the =
new=20
instance. (There is a way to do this, but it's not something you'll be =
wanting=20
to do for a long time, and it won't be covered in this=20
book.)&nbsp;&nbsp;<BR>&nbsp;<BR>&nbsp;As you saw in the original listing =
for=20
class now, which was presented in the preceding chapter (see Listing =
9.2),=20
creating an instance of a class is easy:&nbsp;&nbsp;</DIV>
<DIV><BR>&nbsp;n =3D now()<BR>print "The year is",=20
n.year&nbsp;&nbsp;<BR>&nbsp;</DIV>
<DIV>You may notice that creating an instance of a class looks very much =
like=20
calling a function. That's because it is; Python does a few magic tricks =
behind=20
the scenes, by fabricating a generic object and then passing that as the =
self=20
argument to a function call that effectively looks=20
like&nbsp;&nbsp;<BR>&nbsp;<BR>&nbsp;n =3D=20
now.__init__(object)&nbsp;&nbsp;<BR>&nbsp;<BR>&nbsp;After you've created =
an=20
instance of a class, you might think that you could call the class's =
__init__()=20
method directly, like this:&nbsp;&nbsp;<BR>&nbsp;<BR>&nbsp;n =3D =
now()<BR>o =3D=20
now.__init__(n)<BR>print "year", o.year, n is=20
o&nbsp;&nbsp;<BR>&nbsp;<BR>&nbsp;If you run the preceding code, you'll =
see that=20
you get an AttributeError that tells you that the object o is not an =
instance of=20
class now, but a None object. That's because the self argument you =
provided to=20
__init__() was not really a reference to itself. Only Python can =
construct a=20
generic object that will really work in such a call. Chalk it up to=20
magic."</DIV>
<DIV>&nbsp;</DIV>
<DIV>SH: I'm not sure what that last&nbsp;paragraph means.</DIV>
<DIV>&nbsp;</DIV>
<DIV>Regards,</DIV>
<DIV>Stephen&nbsp;<BR><BR></DIV></FONT></DIV></BODY></HTML>

------=_NextPart_000_0021_01C25108.C47FE720--