944,200 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 567
  • Python RSS
Oct 18th, 2009
0

calling a module from a variable?

Expand Post »
How would i do this?

Python Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 14
Solved Threads: 16
Junior Poster in Training
lukerobi is offline Offline
50 posts
since Sep 2009
Oct 18th, 2009
0
Re: calling a module from a variable?
Im not quite sure what you are doing. But you can do something like this
Python Syntax (Toggle Plain Text)
  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?
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Oct 18th, 2009
1
Re: calling a module from a variable?
Not really sure what exactly you're trying to do...
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Oct 18th, 2009
1
Re: calling a module from a variable?
Quote ...
How would i do this?

Python Syntax (Toggle Plain Text)
  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:

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Oct 18th, 2009
1
Re: calling a module from a variable?
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.

Python Syntax (Toggle Plain Text)
  1. import random
  2.  
  3. class whatever(object):
  4. def __getattr___(self, name):
  5. if name not in locals():
  6. return random.name
Reputation Points: 14
Solved Threads: 16
Junior Poster in Training
lukerobi is offline Offline
50 posts
since Sep 2009
Oct 18th, 2009
3
Re: calling a module from a variable?
Click to Expand / Collapse  Quote originally posted by lukerobi ...
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.

Python Syntax (Toggle Plain Text)
  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)
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,656 posts
since Jul 2008
Oct 18th, 2009
0
Re: calling a module from a variable?
you should use getattr(random, name)
Thanks, thats what i was looking for.
Reputation Points: 14
Solved Threads: 16
Junior Poster in Training
lukerobi is offline Offline
50 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Reversing case out of curiosity.
Next Thread in Python Forum Timeline: List help...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC