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 3 1709
Dave Rose <s_**********@h otmail.com> wrote: 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
>>>>> "Dave Rose" <s_**********@h otmail.com> (DR) wrote: 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(myperso n, attr, val)
--
Piet van Oostrum <pi**@cs.uu.n l>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C 4]
Private email: pi**@vanoostrum .org
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" <pi**@cs.uu.n l> wrote in message
news:m2******** ****@ordesa.cs. uu.nl... >> "Dave Rose" <s_**********@h otmail.com> (DR) wrote:
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(myperso n, attr, val) -- Piet van Oostrum <pi**@cs.uu.n l> URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C 4] Private email: pi**@vanoostrum .org This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Pascal Costanza |
last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Monday, February 13, 2006, VUB Campus Etterbeek
The VUB (Programming Technology Lab, System and Software Engineering
Lab), ULB (deComp) and the Belgian Association for Dynamic Languages
(BADL) are very pleased to invite you to a whole day of presentations
about the programming languages Self, Smalltalk and Common Lisp by
experts in...
|
by: toton |
last post by:
Hi,
I have little confusion about static memory allocation & dynamic
allocation for a cluss member.
I have class like
class Bar{
public:
explicit Bar(){
cout<<"bar default"<<endl;
}
|
by: Guillermo Schwarz |
last post by:
C++ers,
I have programmed in C++ since 1991 and I probably created my best
abstraction layers in C++ in those days. Since 2001 I've been
programming in Java (at first against my will, then I realized the
market was moving into Java) and I'm trying to reimplement in Java
what I did back in those days.
The problem I have is that I have a job offer for programming in C++,
again!!!, and I don't know if I should take it. My problem is...
|
by: jumbojs |
last post by:
I would like some advice on how I should, or if I should break up this class. Right now the class is about 4000 lines of code but it seems hard for me to break up the class. The only downfall I see right now is that it can be a little difficult to read.
Anyway, application takes product information from one database and imports it, after doing some conversion, into another database.
I have some classes to interact with the different...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |