Dynamic classes

D

Dave Rose

Hello all.
I was wondering if creating classes could be dynamic. I want to know if I
can make a class Person, then read in a list of names (say people's names)
so then I can have a class instance created for each name in the list?

Why do I want to do this? I was just thinking if I had a name on the
list, Dave, I could then be able to read the name in the list, and assign
Maria.birthday = <> and all the other attributes I would want to use a class
for, except this is dynamic. I don't know how to iterate thru the list to
assign the different attributes yet, but this seemed reasonable to want to
do, and thought I could learn from this.

Thanks!
Dave
 
A

Alex Martelli

Dave Rose said:
Hello all.
I was wondering if creating classes could be dynamic. I want to know if I
can make a class Person, then read in a list of names (say people's names)
so then I can have a class instance created for each name in the list?

Yes, but what you're asking for in your second sentence is different
from what you're wondering about in your first one.

You can create a class dynamically.
You can create dynamically an instance of a class.
The two things are quite separate issues.

You create a new class each time you execute a 'class' statement, or
call 'type' (or other custom metaclass) with suitable arguments. You
can do either or both in the body of a loop over a list of names, say.

You create a new instance of a class each time you call the class.
Again, of course you can do this in a loop's body.

Creating new classes is a reasonably rare need, creating new instances
is a very common need. Perhaps you can clarify which one you mean?


Alex
 
P

Piet van Oostrum

Dave Rose said:
DR> Hello all.
DR> I was wondering if creating classes could be dynamic. I want to know
DR> if I can make a class Person, then read in a list of names (say
DR> people's names) so then I can have a class instance created for each
DR> name in the list?

If you have the class Person, you are not creating it dynamically. And of
course you can create instances dynamically as you describe.
DR> Why do I want to do this? I was just thinking if I had a name on the
DR> list, Dave, I could then be able to read the name in the list, and
DR> assign Maria.birthday = <> and all the other attributes I would want
DR> to use a class for, except this is dynamic. I don't know how to
DR> iterate thru the list to assign the different attributes yet, but this
DR> seemed reasonable to want to do, and thought I could learn from this.

If I understand you correctly, you want to create a variable with name
'Maria' when you read the name "Maria". Creating variables dynamically is
possible in Python but is almost always the wrong thing to do. Instead it
is usually better to use a dictionary.

class Person:
def __init__(self, name):
self.name = name

persons = {}

now you have a loop that reads persons' names, say in name.

myperson = persons[name] = Person(name)

Now I suppose you want to read additional attributes, while the list of
possible attributes is in principle open.

So suppose you have read the attribute name in attr and the value in val.
The you can dynamically create an instance attribute with:

setattr(myperson, attr, val)
 
D

Dave Rose

Duh! how dumb am I? A dictionary solves all those problems, with each
entry named, and the value of each name could be a class instace. plus all
the class instances can be iterated by a loop.

Thanks Piet & Alex for your guidance!
-Dave

Piet van Oostrum said:
DR> Hello all.
DR> I was wondering if creating classes could be dynamic. I want to know
DR> if I can make a class Person, then read in a list of names (say
DR> people's names) so then I can have a class instance created for each
DR> name in the list?

If you have the class Person, you are not creating it dynamically. And of
course you can create instances dynamically as you describe.
DR> Why do I want to do this? I was just thinking if I had a name on
the
DR> list, Dave, I could then be able to read the name in the list, and
DR> assign Maria.birthday = <> and all the other attributes I would want
DR> to use a class for, except this is dynamic. I don't know how to
DR> iterate thru the list to assign the different attributes yet, but this
DR> seemed reasonable to want to do, and thought I could learn from this.

If I understand you correctly, you want to create a variable with name
'Maria' when you read the name "Maria". Creating variables dynamically is
possible in Python but is almost always the wrong thing to do. Instead it
is usually better to use a dictionary.

class Person:
def __init__(self, name):
self.name = name

persons = {}

now you have a loop that reads persons' names, say in name.

myperson = persons[name] = Person(name)

Now I suppose you want to read additional attributes, while the list of
possible attributes is in principle open.

So suppose you have read the attribute name in attr and the value in val.
The you can dynamically create an instance attribute with:

setattr(myperson, attr, val)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,271
Messages
2,571,357
Members
48,042
Latest member
DoraMcBrie

Latest Threads

Top