[Tutor] changing mutable object from within a class

kromag@nsacom.net kromag@nsacom.net
Mon, 5 Nov 2001 00:55:48 -0800 (PST)


Howdy folks!

I am having difficulty understanding whether or not I can make a global 
object mutable from within a class.

The script is a 'citadel' bbs clone. Citadel's read and enter messages into 
messagebases called rooms. My rooms consist of pickled dictionaries full of 
lists:

roomname.room -> {1: username, time of entry, post}

Citadels traditionally start from the 'lobby'. That means I need to load and 
read the lobby first by default, then read from other rooms I 'jump' to.

The problem is: I can't for the life of me figure out how to pass the 
new 'roomname' to the command prompt after I choose it!

I think my problem is that the __init__ function is only reading the initial 
global declaration of 'roomname'.

I have painted myself into a corner! Throw some turpentine on me! :-)

---------------at least I know this works!-------------------

>>> def room_namer(roomname=None):
        if roomname is None:
                roomname='lobby'
        return roomname

>>> roomname=room_namer()
>>> roomname
'lobby'

roomname=room_namer('new')
>>> roomname
'new'


--------------script----------------------------

def get_roomlist():
        roomlistsuck=glob.glob('*.room')
        roomlist=[]
        for all in roomlistsuck:
                roomlist.append(all[:-5])
        return roomlist

def room_namer(roomname=None):
        if roomname is None:
                roomname='lobby'
        return roomname

roomname=room_namer()

'''This is the meat of the matter
all commands (and their helpfiles) are
located here. There are some extra
underscores floating around ignore
them please! I think I have plans....'''

class Jabulon(cmd.Cmd):

        def __init__(self):

#fire up that command prompt!

                self.prompt='%s ('M' for menu, Help): '%(room_namer
(roomname))
                self.post={}

        def help_m(self):
                print 'Prints a handy menu.'
        def do_m(self, _):
                print '*'*50
                print '(R)ead New Messages, (E)nter Message'
                #print '(S)kip This Room (G)o to Next Room '
                print '(L)ist Rooms (C)reate New Room.'
                print '(T)erminate Connection'
                print 'Help (letter) for short explaination.'
                print '*'*50


#List rooms.
        def help_l(self):
                print 'Prints the roomlist'
        def do_l(self,_):
                roomlist=get_roomlist()
                print 'n%sn%snn%sn' %('Roomlist','='*8,roomlist)

#Jump to another room.

        def help_j(self):
                print 'Jumps to another room.'
        def do_j(self,_):
                roomlist=get_roomlist()
                next_room=raw_input('Jump to which room? : ')
                if next_room in roomlist:
                        roomname=room_namer(next_room)
                else:
                        print '%s not found.'%(next_room)

#bring the noise!

if __name__=='__main__':
        jab=Jabulon()
        jab.cmdloop()

---------------------end script-----------------------