[Tutor] Class stuff

alan.gauld@bt.com alan.gauld@bt.com
Thu, 23 Nov 2000 17:29:44 -0000

> I have a regular old dictionary called Rooms, and a class called Room.

I assume you have actually created an empty dictionary somewhere:

Rooms = {}

> Room is defined as follows -
> class Room:
> 	def __init__(self,x,y,z):
> 	self.Position=(x,y,z)
> 	self.this=self
> 	if Rooms.has_key(str(x)+','+str(y)+','+str(z)):
> 		print 'Room already exists'
> 	else:
> 		Rooms[str(x)+','+str(y)+','+str(z)]=self

Assuming the indentation is only wrong in the mail 
- otherwise youd get a syntax err I suspect....

it looks OK.

How exactly are you using this? Is it in a module?
Is the Rooms declaration in the same module, in other 
words are you doing something like this:

>>> import room
>>> Rooms = {}
>>> for i in range(5):

>>> Rooms

Or is it all in a single file? Or even all at the >>> prompt?

> 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)"

I'm not clear what your doing, Heres a snapshot of an interactive session:

>>> r = {}
>>> class c:
...  def __init__(s,i):
...    r[i] = s
>>> r
>>> c(1)
<__main__.c instance at 7f77c0>
>>> r
{1: <__main__.c instance at 7f77c0>}

Seems to do what you are trying to do?

> I've debugged it down to the last line, so that referencing 
> the class as self does not work. 

It should do.

Alan G.