473,403 Members | 2,284 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,403 software developers and data experts.

Dynamically created objects

Hi there.

I need to create objects on the fly in my program. The names of the
objects must be unique, obviously, and I need to put them in a list for
later use.

How do i set the name of an object if the name is stored in another
variable?

I've looked in the O'Reiley Python Cookbook and on google, but no joy.

Thanks in advance.

/Tram
Dec 28 '07 #1
2 1139
En Fri, 28 Dec 2007 04:14:43 -0300, Michael Bernhard Arp Sørensen
<mu*************@gmail.comescribió:
I need to create objects on the fly in my program. The names of the
objects must be unique, obviously, and I need to put them in a list for
later use.

How do i set the name of an object if the name is stored in another
variable?

I've looked in the O'Reiley Python Cookbook and on google, but no joy.
Use a dictionary as a container.

pyns = {}
pyname = "Joe"
pyo = object()
pyns[name] = o
pyanother = set("another")
pyns["another"] = another
pyns
{'Joe': <object object at 0x009D0478>,
'another': set(['a', 'e', 'h', 'o', 'n', 'r', 't'])}

--
Gabriel Genellina

Dec 28 '07 #2
On Fri, 28 Dec 2007 08:14:43 +0100, Michael Bernhard Arp Sørensen wrote:
Hi there.

I need to create objects on the fly in my program. The names of the
objects must be unique, obviously, and I need to put them in a list for
later use.
Why do you think they need names? If you have them in a list, just refer
to them by the list and index. Or a dict and a key.

E.g. instead of this:
# this doesn't work
for i in range(10):
"foo" + i = "some data here" # variables like foo0, foo1 ...
process(foo0, foo1, foo2)

do this:

foo = []
for i in range(10):
foo.append("some data here")
process(foo[0], foo[1], foo[2])

How do i set the name of an object if the name is stored in another
variable?

If you really need to do this, and I doubt that you do, here's one way:

>>bird
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'bird' is not defined
>>globals()["bird"] = "a parrot with beautiful plumage"
bird
'a parrot with beautiful plumage'
If you're tempted to try the same trick with locals(), don't bother -- it
won't reliably work.

If you are absolutely sure that the data is safe (i.e. you control it,
not random users via a web form) you can also use exec.

--
Steven
Dec 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: sirsean | last post by:
Hi all. I'm trying to dynamically build menus and objects after my page loads. Data is stored in an XML file and is parsed at runtime into Javascript objects. At the moment, I'm working on creating...
2
by: Thomas W. Brown | last post by:
If I am using the CSharpCodeProvider to dynamically compile an in-memory assembly from some C# source, do I need to worry about signing this assembly if I'm doing the compilation, instantiation,...
4
by: Ray | last post by:
I want to dynamically load DLLs (created from VB) and instantiate a class with a particular name, like "ProcessClass". I am able to load the DLL and confirm there is a class by that name BUT I...
2
by: Colin McGuigan | last post by:
This is a translation of an ASP page to ASP.Net. First, the background: The goal is to have a grid of different settings for the application -- think something along the lines of the Property...
7
by: Michael | last post by:
Hi, What's the benefit to dynamically allocate memory? using namespace std; int main() { char* ptr; ptr="abc";
12
by: vbnewbie | last post by:
I am having problems accessing properties of dynamically generated objects in VB2005. Can someone please help? In a nutshell: My app creates an equal number of checkboxes and labels that share the...
10
by: Jess | last post by:
Hello, I have a program that stores dynamically created objects into a vector. #include<iostream> #include<vector> using namespace std;
2
by: Dwight Johnson | last post by:
I am building a website in VS2005. I basically have one page, default.aspx, which contains placeholder objects into which I add various controls that are dynamically created. I have a dropdownlist...
1
by: krishna81m | last post by:
I am unable to create a list of objects dynamically. I run a SQL query on the database and populate objects of types that I also know aprior. I read the query as a string, run it on the database and...
29
by: Quarco | last post by:
This one is driving me nuts.... var tbl = document.createElement("table"); var tbody = document.createElement("tbody"); for(var i=0; i<10; i++) { var row =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.