Passing default class attributes across modules

Paul Simmonds psimmo60 at hotmail.com
Wed Oct 23 08:57:20 EDT 2002


Greetings all,

I've been working on this problem for a while now, and I'm not getting 
anywhere, so I think it's time I asked for some help.

I've got a Tkinter Dialog box, which takes a list of class 'pointers' (I'm 
not sure what they're really called, that's just my C background creeping 
in) and it prints out all the class attributes in two side by side listboxes 
showing name and value, and allows the user to examine the contents of every 
member of the list. The Dialog itself allows the user to add/remove 
attributes to individual classes, or to add/remove class instances from the 
list.

I managed to get the dialog working and everything for a single class def, 
but it bulked out the code in the master GUI window module, which I was 
intending to be a stepping stone/throw away prototype. So, I've moved the 
code into a general widgets module, and with a couple of changes, it works 
fine...except for the adding of new class instances.

My classes are defined as the example below:

class Knight(object):
    name="Lancelot"
    title="Sir"
    favcolour="Blue"

What I really want is a way to pass the default values so I can put them 
straight into a new class instance. I have tried making the first member of 
the class list a default object, then putting the values from that into a 
blank class:

<code begins>
import tkSimpleDialog
class MemberDialog(tkSimpleDialog.Dialog):
    def __init__(self,parent=None,title=None,list=[]):
        self.toplist=list #List of classes-[0] contains defaults
    ##<lots of code>
    # Adds new program to registry
    def onNew(self):
        name=tkSimpleDialog.askstring("New Entry",
	                    "Type the name of the new registry entry")
	if name:
	    self.toplist.append(self.BlankClass())
            defaultcontents=dir(self.toplist[0])\
                    [14:len(dir(self.toplist[0]))]
	    for entry in range(14,len(defaultcontents)):
	    	self.toplist[len(self.toplist)-1].\
                        __setattr__(contents[entry],self.toplist[0].\
                        __getattribute__(defaultcontents[entry]))
    <code snipped>

    class BlankClass(object):
        pass
<code end>
The code above assumes there's only ever going to be 14 builtin methods in a 
new-style class. If there's a quick fix for that, please let me know.

When I run the code above with a class similar to the example, then later 
try to access the '.name' attribute of the new instance, I get a 'BlankClass 
object has no attribute name', and, sure enough, when I print the dir() 
contents of the last member of self.toplist, only the object builtin methods 
are shown. Any hints would be most welcome.

Thanks in advance,
Paul

**********************************************
I used to have a handle on life, but it broke.
**********************************************

_________________________________________________________________
Get a speedy connection with MSN Broadband.  Join now! 
http://resourcecenter.msn.com/access/plans/freeactivation.asp





More information about the Python-list mailing list