473,748 Members | 9,913 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 2414
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
2444
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
2766
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
2312
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
2795
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
1542
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
2546
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
1705
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
2105
by: raashid bhatt | last post by:
does having more variables increases the size of program.
0
8987
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
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,...
1
9316
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,...
1
6793
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
6073
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
4597
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...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
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
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.