473,324 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Dynamic naming of class instances

17
Im currently writing an application witch is going to use lots of instances of the same class.( i hope i got that terminology right)
So instead of naming them by my self when they are started i need python to do it for me.

Say it looks something like this

Expand|Select|Wrap|Line Numbers
  1. instance = myClass()
when a new instance is started i want it to be named "instance2"next "instance3" and so on. I cant do it like this can i?:
Expand|Select|Wrap|Line Numbers
  1. instance+cnt = myClass()     # cnt is a variable that holds the number of instances
How can i name it after the content of a variable?

I am really sorry if this sounds very confusing, i will try to explain it better if you guys dont get it.
Thanks
Feb 27 '08 #1
5 2526
bvdet
2,851 Expert Mod 2GB
Im currently writing an application witch is going to use lots of instances of the same class.( i hope i got that terminology right)
So instead of naming them by my self when they are started i need python to do it for me.

Say it looks something like this

Expand|Select|Wrap|Line Numbers
  1. instance = myClass()
when a new instance is started i want it to be named "instance2"next "instance3" and so on. I cant do it like this can i?:
Expand|Select|Wrap|Line Numbers
  1. instance+cnt = myClass()     # cnt is a variable that holds the number of instances
How can i name it after the content of a variable?

I am really sorry if this sounds very confusing, i will try to explain it better if you guys dont get it.
Thanks
For a class named 'Player()':
Expand|Select|Wrap|Line Numbers
  1. >>> for i in range(5):
  2. ...     exec 'player%d = Player()' % i
  3. ...     
  4. >>> player1
  5. <__main__.Player instance at 0x00F4FF80>
  6. >>> player2
  7. <__main__.Player instance at 0x00F4FDF0>
  8. >>> 
Feb 27 '08 #2
Smygis
126 100+
Its simple, don't do it. Use a list or a dictionary.
Because how are you going to read the variables later?
A try-except block in a loop or something will work. But what happens is that you need something about 10 lines of code to do something that can be done in a more effective way on 3-4 lines of code. (Ok, thats someting of a lie, You need a little more code to do it, yes. but not that much.)

Expand|Select|Wrap|Line Numbers
  1. >>> class myClass:
  2. ...     pass
  3. ... 
  4. >>> dd = {}
  5. >>> for i in range(5):
  6. ...     dd["instance%d" % i] = myClass()
  7. ... 
  8. >>> dd["instance3"]
  9. <__main__.myClass instance at 0xb7cdfcec>
  10. >>> dd["instance2"]
  11. <__main__.myClass instance at 0xb7cdfc6c>
  12. >>> 
  13.  
It can be done that other way but its not so 'pythonic' to do it like that.
Feb 27 '08 #3
Moezzie
17
Thanks, that really helped Smygis.

Now i've kind of run into another problem with this...
How do i call one of the instances later on in my code?
Its kind of confusing. I havnt used classes in this way before so it might take some time to get the new thinking in.
Mar 6 '08 #4
bvdet
2,851 Expert Mod 2GB
Thanks, that really helped Smygis.

Now i've kind of run into another problem with this...
How do i call one of the instances later on in my code?
Its kind of confusing. I havnt used classes in this way before so it might take some time to get the new thinking in.
I am not sure what Smygis has in mind. You can create a dictionary of instances with an incremented name as the dictionary key.
Expand|Select|Wrap|Line Numbers
  1. # Create dictionary of Players()
  2. n = 10
  3. dd = {}
  4. for i in range(n):
  5.     dd['player%d' % (i+1)] = Player()
  6.  
  7. print dd['player1'].name
  8. print dd['player1'].score
  9. globals().update(dd)
  10. print player2.name
  11. print player2.score
  12.  
  13. >>> Tom Berry
  14. 394
  15. Dick Smith
  16. 70
  17. >>> 
Mar 6 '08 #5
Moezzie
17
I am not sure what Smygis has in mind. You can create a dictionary of instances with an incremented name as the dictionary key.
Expand|Select|Wrap|Line Numbers
  1. # Create dictionary of Players()
  2. n = 10
  3. dd = {}
  4. for i in range(n):
  5.     dd['player%d' % (i+1)] = Player()
  6.  
  7. print dd['player1'].name
  8. print dd['player1'].score
  9. globals().update(dd)
  10. print player2.name
  11. print player2.score
  12.  
  13. >>> Tom Berry
  14. 394
  15. Dick Smith
  16. 70
  17. >>> 
Well, that seems easy enough :p
Thanks for the help. It really wasnt as hard as i imagined it. Im gonna try to implement this into my code somehow.
Mar 7 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Mark Broadbent | last post by:
stupid question time again to most of you experts but this is something that continually bothers me. I am trying to get into the habit of naming variables and controls in an assembly as per...
8
by: Lance | last post by:
I've been teaching myself C# for a few months now and I have a concept problem. I know how to instantiate an object from a class I’ve created, but now I want to retrieve and store data in a...
4
by: michaeltorus | last post by:
H I've got an n-tier web app. The namespaces follow the layers in that I've got a DAL namespace, a BOL namespace and a WEB namespac What I'm tring to decide is the naming convention for my...
0
by: Carl Colijn | last post by:
Hi all, Disclaimer: before I might trigger your "let's start a holy war!" button, I'd like to say I'm not intended to; I just post this message to get some input and not to promote "Yet Another...
9
by: kevininstructor | last post by:
Greetings, I am in the process of creating naming conventions for VB.NET controls i.e. CheckBox -> chkShowThisOnStartup ListBoxt -> lstSomeList What I am looking for other developers...
4
by: Zark3 | last post by:
Hi all, I was wondering if anybody could enlighten me on the possibility of dynamic casting. Or, well, whether or not I'm actually trying to do this the right way. What I have is a base class...
35
by: Smithers | last post by:
Is it common practise to begin the name of form classes with "frm" (e.g., frmOneForm, frmAnotherForm). Or is that generally considered an outdated convention? If not "frm" what is a common or...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
1
by: Moezzie | last post by:
How would i go about to have class instances named after the content of a sertain variable? Ive posted the same question a couple of weeks back regarding Python. This time i want to do it in C++...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.