473,416 Members | 1,739 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,416 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 2533
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++...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
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...
0
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...

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.