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

Import Question ..

Hello,

I have a question on import. I have a file which contains some assorted lists, and some other definitions. But all of the list names have a common part to it and a specific part in the beginning. For example:

# file containing definitions 'def.py'

firstlistCommonPart = []

secondlistCommonPart = []
Now what i want to do is:

# some other file 'some.py'

import def

def dosomething(something)

if something == 'firstlist':
listdef = def.firstlistCommonPart
elif something == 'secondlist':
listdef = def.secondlistCommonPart

This is quite frustrating if i have lots of definitions. So is there a way where i could get something like:

def dosomething(something):

str = something+CommonPart # something is passed as a string
listdef = def.str

basically i am asking if i can pass the string value to get an attribute from the def file. I tried doing something like in the above case, but it returns and tells me def has no attribute str.

Any help would be much appreciated.

Thanks,
Vinod
__________________________________________________ ________________
Switch to Netscape Internet Service.
As low as $9.95 a month -- Sign up today at http://isp.netscape.com/register

Netscape. Just the Net You Need.

New! Netscape Toolbar for Internet Explorer
Search from anywhere on the Web and block those annoying pop-ups.
Download now at http://channels.netscape.com/ns/search/install.jsp
Jul 18 '05 #1
4 1182
Sk*********@netscape.net wrote:
import def

def dosomething(something)

if something == 'firstlist':
listdef = def.firstlistCommonPart
elif something == 'secondlist':
listdef = def.secondlistCommonPart

This is quite frustrating if i have lots of definitions. So is there a way
where i could get something like:


Now 'def' is a _really_ bad name for a module - I'll use 'module' instead.

import module

def dosomething(something):
listdef = getattr(module, something + "CommonPart")

Peter

Jul 18 '05 #2
On Monday 12 July 2004 1:32 pm, Peter Otten wrote:
Sk*********@netscape.net wrote:
import def

def dosomething(something)

if something == 'firstlist':
listdef = def.firstlistCommonPart
elif something == 'secondlist':
listdef = def.secondlistCommonPart

This is quite frustrating if i have lots of definitions. So is there a
way where i could get something like:


Now 'def' is a _really_ bad name for a module - I'll use 'module' instead.

import module

def dosomething(something):
listdef = getattr(module, something + "CommonPart")

Peter


Yes, "getattr" is the answer. This came while I was replying so I'll just add
that "str" is also a bad name for a variable (it was in the OP), since it
masks a built in name. "def" is in fact an impossible name for a module:
"import def" is a syntax error (as is "def.firstlistCommonPart") because
"def" is a reserved keyword.

James
--
James Henderson, Logical Progression Ltd
http://www.logicalprogression.net
http://mailmanager.sourceforge.net

Jul 18 '05 #3
James Henderson wrote:
add that "str" is also a bad name for a variable (it was in the OP), since


Read

http://mail.python.org/pipermail/pyt...ly/045948.html

on that and become acquiescent...

Peter
Jul 18 '05 #4
On Monday 12 July 2004 1:57 pm, Peter Otten wrote:
James Henderson wrote:
add that "str" is also a bad name for a variable (it was in the OP),
since


Read

http://mail.python.org/pipermail/pyt...ly/045948.html

on that and become acquiescent...

Peter


This is what Guido wrote there:

(Not that it isn't a good idea to avoid obvious clashes --
'str' for string variables and 'type' for type variables being the
most obvious stumbling blocks.)

I can accept that. :)

(Actually, in my haste I didn't notice that "str" was being defined inside a
function. As a global it really would be a no-no.)

James
--
James Henderson, Logical Progression Ltd
http://www.logicalprogression.net
http://mailmanager.sourceforge.net

Jul 18 '05 #5

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

Similar topics

0
by: Stian Søiland | last post by:
all examples performed with: Python 2.3+ (#2, Aug 10 2003, 11:09:33) on linux2 (2, 3, 0, 'final', 1) This is a recursive import:
0
by: Vio | last post by:
Hi, I've been trying to embed (statically) wxPy alongside an embedded py interpreter on a linux/gtk box. At one point, for some reason misc.o linking reported "multiple definitions of...
0
by: John Roth | last post by:
I've found a case where it seems that Python is importing two copies of a module without any reason or indication. It took me a while to verify that this is what is occuring: I had to write a...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
1
by: mark | last post by:
In Access 2000 and 2002, I have created an import specification to import the fixed-width recordset below into an existing table. I am having strange problems with the import of the date and time...
4
by: Bruce W. Roeser | last post by:
All, I'm reading a book by Charles Petzold (Programming VS.Net). Pretty good content but am confused about the difference. From the text: ...
2
by: Jon | last post by:
It appears that (windows) python searches in the current working directory before looking in the local site-packages directory, or that '.' comes first in sys.path? The problem arises when I made...
7
by: Ron Adam | last post by:
from __future__ import absolute_import Is there a way to check if this is working? I get the same results with or without it. Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win 32 ...
5
by: W. Watson | last post by:
Is there a single source that explains these statements? ------------------------------ from Tkinter import * from Numeric import * import Image import ImageChops import ImageTk import time...
9
by: rsoh.woodhouse | last post by:
Hi, I'm trying to work out some strange (to me) behaviour that I see when running a python script in two different ways (I've inherited some code that needs to be maintained and integrated with...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...

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.