473,698 Members | 2,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

automatic python variables

8 New Member
I'm looking for ways to create variables in python3 as needed. E.g

book1 = book(name1) # book is a class
book2 = book(name2)
book3 = book(name3)
book4 = book(name4)
book5 = book(name5)
:
:
:
V
book-n = book(name-n)

I have the names of the book in a list but want to load those into class instances to take advantage off OOP. I could assign five to ten class objects like above but what if I have up a 100 books to deal with. Any way to generate those variables? (Note: Its not a code about books ... the above is a sample scenario)
Dec 25 '10 #1
4 2409
bvdet
2,851 Recognized Expert Moderator Specialist
If your code is encapsulated in a class instance:
Expand|Select|Wrap|Line Numbers
  1.     for i, name in enumerate(book_list):
  2.         setattr(self, "book%s" % (i), name)
If not:
Expand|Select|Wrap|Line Numbers
  1. for i, name in enumerate(book_list):
  2.     exec "%s = %s" % ("book%s" % (i), name)
It's better to maintain the list of books in a list or dictionary instead of creating a variable for each one.
Dec 27 '10 #2
twohot
8 New Member
And what if you plan to do more than list the books? Say, be able to access each book's properties eg. chapters, publisher, author, genre, revisions. Would a dictionary still do?

Wouldn't it be better to have a list of object 'books' where the enumeration serves as a serial/key. eg book[1],book[2] ....book[n], each managing its own little db? or is SQL better off at this kind of thing?

Just thinking ...:o, Like I said, its really not about books. What I'm doing is more dynamic. The next time the code runs, those serials need not be the same for each item
Dec 27 '10 #3
bvdet
2,851 Recognized Expert Moderator Specialist
You could create a class instance for each book to contain the book data. Maintaining the data in an XML or SQL file may be the best route for significant amounts of data.
Dec 27 '10 #4
twohot
8 New Member
That's what I thought ... since I don't anticipate anything more than a few thousands of files-lines (for the properties and not instances .....well, looking at about 10 to 20 instances at most). I'd probably go the 'class' way. I don't really plan to hold much in memory anyway, so the class object overhead shouldn't be significant.
Dec 27 '10 #5

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

Similar topics

0
2441
by: Rasmus Fogh | last post by:
Someone raised the question of automatic code generation a few weeks back. And yes, we (CCPN) are using automatic Python code generation in a major way. Basically we are making data models in UML, and using automatic code generation to make Python APIs, XML I/O etc. (more below). We can be found at http://www.ccpn.ac.uk/index.html As a general point, automtic code generation would seem like a good idea in special cases where:
3
2762
by: prabhu | last post by:
hello to all, Please can anybody tell me the differnece between static and ordinary member variables. thankyou in advance, vishnu
15
2306
by: pranab_bajpai | last post by:
So I want to define a method that takes a "boolean" in a module, eg. def getDBName(l2): .... Now, in Python variables are bound to types when used, right? Eg. x = 10 # makes it an INT whereas
1
12880
by: Giao | last post by:
Hi, I'd like to do the following things in python script x = "file1" y = "file2" shell's commands x y Is any one out there tell me how it get done. Thanks
31
2781
by: Mark Dufour | last post by:
Hi all, I have recently released version 0.0.20 and 0.0.21 of Shed Skin, an optimizing Python-to-C++ compiler. Shed Skin allows for translation of pure (unmodified), implicitly statically typed Python programs into optimized C++, and hence, highly optimized machine language. Besides many bug fixes and optimizations, these releases add the following changes: -support for 'bisect', 'collections.deque' and 'string.maketrans'
7
1541
by: MD | last post by:
Hi, I would like to access "variables" defined in my Python program in a C module extension for Python. Is this possible? I looked at the Python C API reference but didn't find anything there that could help me. Thanks in advance for any help/tips. Regards, -MD
41
2538
by: none | last post by:
Hello, IIRC, I once saw an explanation how Python doesn't have "variables" in the sense that, say, C does, and instead has bindings from names to objects. Does anyone have a link? Thanks, Ami
4
1702
by: acw | last post by:
I am trying to understand how IE handles automatic tag variables. I know that IE will create a global variable each time it finds a tag with its name or id attribute set. If you have more than one tag with the same name or id (yes this is bad) on a page then that global variable goes from being a tag reference to being a collection of references. In most cases if you remove the duplicate tag the global var becomes a tag reference...
9
2100
by: raashid bhatt | last post by:
does having more variables increases the size of program.
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8876
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6531
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 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...
0
5867
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();...
0
4372
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...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
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...

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.