The Artima Developer Community
Sponsored Link

Python Answers Forum
How to call a class where it's name passed from variable

1 reply on 1 page. Most recent reply: Jan 17, 2007 10:26 PM by Christophe

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Baron Teh

Posts: 4
Nickname: baron
Registered: Jan, 2005

How to call a class where it's name passed from variable Posted: Nov 9, 2006 5:41 PM
Reply to this message Reply
Advertisement
Hello all,

I encountered a problem similar to following situation:

1. I have a variable. eg: var1='path'

2. I imported sys module. eg: import sys

3. I need to print sys.path. eg: print sys.path

4. However, i need to pass the "path" from the variable. It becomes : print sys.var1 . However this will cause error.

May i know how shall i resolve this? As my variable is array and i need to recursively call the classes named in the array.

Thanks.


Christophe

Posts: 3
Nickname: mccricri
Registered: Jan, 2004

Re: How to call a class where it's name passed from variable Posted: Jan 17, 2007 10:26 PM
Reply to this message Reply
You can do this :

import sys
var1 = "path"
getattr(sys,var1)

Flat View: This topic has 1 reply on 1 page
Topic: Python with xlib: Control pointer without mouse Previous Topic   Next Topic Topic: python this var, who joke

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use