calling a module from a variable?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 50
Reputation: lukerobi is an unknown quantity at this point 
Solved Threads: 16
lukerobi lukerobi is offline Offline
Junior Poster in Training

calling a module from a variable?

 
0
  #1
Oct 18th, 2009
How would i do this?

  1. import random
  2.  
  3. name = random
  4.  
  5. random.name

I dont cant to use name = random.random, as name is something that could change
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 920
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345
 
0
  #2
Oct 18th, 2009
Im not quite sure what you are doing. But you can do something like this
  1. import random as name
  2. name.random()
  3. name.randrange(1,50)
  4. #etc
Hope that helps if not, could you explain what you mean a bit more in depth?
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,613
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso
 
1
  #3
Oct 18th, 2009
Not really sure what exactly you're trying to do...
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster
 
1
  #4
Oct 18th, 2009
How would i do this?

  1. import random
  2.  
  3. name = random
  4.  
  5. random.name

I can't use 'name = random.random', as name is something that could change.
No, you can't do something like that.

But you can do:

  1. from random import *
  2.  
  3. name = random()
  4.  
  5. print (name)

But I'm afraid that's the best you're going to do.
Last edited by AutoPython; Oct 18th, 2009 at 11:41 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 50
Reputation: lukerobi is an unknown quantity at this point 
Solved Threads: 16
lukerobi lukerobi is offline Offline
Junior Poster in Training
 
1
  #5
Oct 18th, 2009
thanks for the replies, but unfortunatly none of thee will work.

I am basically trying to return a function from a library if it is not in a class.

  1. import random
  2.  
  3. class whatever(object):
  4. def __getattr___(self, name):
  5. if name not in locals():
  6. return random.name
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 948
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 217
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
3
  #6
Oct 18th, 2009
Originally Posted by lukerobi View Post
thanks for the replies, but unfortunatly none of thee will work.

I am basically trying to return a function from a library if it is not in a class.

  1. import random
  2.  
  3. class whatever(object):
  4. def __getattr___(self, name):
  5. if name not in locals():
  6. return random.name
you should use getattr(random, name)
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 50
Reputation: lukerobi is an unknown quantity at this point 
Solved Threads: 16
lukerobi lukerobi is offline Offline
Junior Poster in Training
 
0
  #7
Oct 18th, 2009
Originally Posted by Gribouillis View Post
you should use getattr(random, name)
Thanks, thats what i was looking for.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC