[Tutor] Class stuff

Kalle Svensson kalle@gnupung.net
Thu, 23 Nov 2000 20:40:22 +0100


--oyUTqETQ0mS9luUI
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Sez Richard Wilkins:
> Hello everyone,

Hi!

> I'm _positive_ it's a newbie question, so brace yourselves.
>=20
> I have a regular old dictionary called Rooms, and a class called Room.

This dictionary is created somewhere else?  I would suggest passing it as a
parameter to the constructor.  Otherwise it'll have to be a global, and you
never know about globals... ;)  Anyway, look at the bottom for another
solution.

Also, it think it is customary not to use capital letters in variable names,
at least not the first character.  This is a matter of taste, of course, but
I find it helps to save the capitals for class names.

> Room is defined as follows -
>=20
> class Room:
> 	def __init__(self,x,y,z):
> 	        self.Position=3D(x,y,z)
> 	        self.this=3Dself

What is this for?  It creates a circular reference:
self has a reference to this that is a reference to self that has a
reference to this that is a reference to self that ...

> 	        if Rooms.has_key(str(x)+','+str(y)+','+str(z)):
> 		        print 'Room already exists'
> 	        else:
> 		        Rooms[str(x)+','+str(y)+','+str(z)]=3Dself
>=20
>=20
> When I create an object it all works fine. When I type Rooms at the
> interpreter prompt, it says:
> "call of non-function (type None)"
>=20
> I've debugged it down to the last line, so that referencing the class as
> self does not work. I need to know how to reference the class from
> within itself. I don't want to have to provide a function that wraps the
> class...:(

The class and the instance are two different things.  If you want to
reference tha instance, self should work.  The class (Room, in this case)
is available through instance.__class__.  An example *might* make it cleare=
r:

>>> class Point:
=2E..     def __init__(self, x, y):
=2E..         self.x, self.y =3D x, y
=2E..         print "Created with x =3D", x, ", y =3D", y
=2E..     def pp(self):
=2E..         print "Coordinates are", self.x, "and", self.y
=2E..     def hi(self):
=2E..         print self, "is an instance of", self.__class__
=2E..=20
>>> p =3D Point(1, 1)
Created with x =3D 1 , y =3D 1
>>> p.hi()
<__main__.Point instance at 8072688> is an instance of __main__.Point
>>> p.pp()
Coordinates are 1 and 1
>>> p.__class__
<class __main__.Point at 80722f8>
>>> p
<__main__.Point instance at 8072688>
>>> p.x
1
>>>=20


> Can someone please help me with my problem?
>=20

I don't know if it is much help, but:
Your code checks if the room exists in the dictionary, and adds it if not,
right?  To me, it feels a bit weird to let the class handle where it is
stored.  I'd suggest another method of controlling your rooms, introducing
another class.  You might call it Dungeon or House or something.
Example:

class House:
    def __init__(self):
        self.rooms =3D {}
    def add_room(self, room):
        if self.rooms.has_key("%s,%s,%s" % room.position):
	    print 'Room already exists'
	else:
	    self.rooms["%s,%s,%s" % room.position] =3D room
	    print 'OK'
	   =20
class Room:
    def __init__(self, x, y, z):
        self.position =3D (x, y, z)

Then, you could do something like:
>>> h =3D House()
>>> r =3D Room(1, 1, 1)
>>> h.add_room(r)
OK
>>> h.add_room(r)
Room already exists
>>> h.rooms["1,1,1"]
<__main__.Room instance at 8073298>
>>> r
<__main__.Room instance at 8073298>
>>>=20

HTH,
  Kalle
--=20
Email: kalle@gnupung.net     | You can tune a filesystem, but you
Web: http://www.gnupung.net/ | can't tune a fish. -- man tunefs(8)
PGP fingerprint: 0C56 B171 8159 327F 1824 F5DE 74D7 80D7 BF3B B1DD

--oyUTqETQ0mS9luUI
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.2 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE6HXKmdNeA1787sd0RAmTAAJ9EXleR0gEfbTnG2QUa3jJu+nuJNwCcDycp
u+f+DvjouiM7xvlFIJ38d2A=
=gsHC
-----END PGP SIGNATURE-----

--oyUTqETQ0mS9luUI--