2Q's: How to autocreate instance of class;How to check for membership in a class
asdf at asdf.com
Tue Jun 17 03:16:54 CEST 2008
So I'm writing a script which will create several instances of User()
class. I want each instance to be named after the login name
of a user. I don't know beforehand how many users the script will
have to create or how they are named. Right now I've created a dictionary
of usernames as keys and objects themselves as values. It works,
but is very unwieldy, because every time I need to access a value from
within an object I have to do something like dict-user[x].value.
Is there a way of automatically naming objects from variable names.
So for example if I know that var1=jsmith. Can I somehow do
var1=User(). This obviously won't work because I tried this. It'll just
create var1 of type User.
My second question is how can I check if object is a member of a class.
so let's say I create a=User(), b=User()...
Can I do something similar to
print "user already created"
Right now I'm doing this using try-except which works
but I'm looking for something cleaner.
thanks for all the replies.
More information about the Python-list