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

Confused about hasattr/getattr/namespaces

I'm confused about the use of hasattr/getattr, or possibly
namespaces. I know how to do this:

class UnderstandThis(object):
def do_foo(self): pass
def do_bar(self): pass
def doit(self, cmd):
funcname = 'do_' + cmd
if hasattr(self, funcname):
getattr(self, funcname)()
else:
print 'not found'

But if I try to do this with classes in a module (instead of
functions in a class):

class FooGenerator(object): ...
class BarGenerator(object): ...

def generate(cmd):
clsname = cmd + 'Generator'
if hasattr(???, clsname):
etc...

I don't know what to use for ??? instead of self. If I print
globals() inside generate() it shows the two classes, but using
it in the hasattr line doesn't work -- huh? I can make it work
by wrapping the generate function in a (otherwise useless) class
and using self -- again, huh?

Couldn't find an explanation or example in the docs. In fact,
the description of vars/globals/locals (in section 2.1) just
confused me even more. Think I'm heading down the wrong path.
Can somebody please un-confuse me?

Brian.
Jul 18 '05 #1
2 2596
On 2004-02-29 13:48:21 -0500, br***@mirror.org (Brian Roberts) said:
I'm confused about the use of hasattr/getattr, or possibly
namespaces. I know how to do this:

class UnderstandThis(object):
def do_foo(self): pass
def do_bar(self): pass
def doit(self, cmd):
funcname = 'do_' + cmd
if hasattr(self, funcname):
getattr(self, funcname)()
else:
print 'not found'

But if I try to do this with classes in a module (instead of
functions in a class):

class FooGenerator(object): ...
class BarGenerator(object): ...

def generate(cmd):
clsname = cmd + 'Generator'
if hasattr(???, clsname):
etc...

I don't know what to use for ??? instead of self. If I print
globals() inside generate() it shows the two classes, but using
it in the hasattr line doesn't work -- huh? I can make it work
by wrapping the generate function in a (otherwise useless) class
and using self -- again, huh?

Couldn't find an explanation or example in the docs. In fact,
the description of vars/globals/locals (in section 2.1) just
confused me even more. Think I'm heading down the wrong path.
Can somebody please un-confuse me?


if you have the module object, you can use that.. or you could use
sys.modules[__name__] to get the current module (import sys, of course).

globals(), locals() are dictionaries, so you would have to do "clsobj =
globals().get(clsname)" or something like that. You *probably* want
something more explicit than that though.. it probably wouldn't be too
much work to keep your own command->class dictionary.

-bob

Jul 18 '05 #2
Here's one way:
def generate(cmd):
clsname = cmd + "Generator"
g = globals()
if g.has_key(clsname): ...
globals() returns the module's dict, not the module. You could also
import modulename as self
but this is a kind of circular import, and the re-use of the name "self"
(which will be used in method definitions) smells bad too.

Jeff

Jul 18 '05 #3

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

Similar topics

2
by: Dragos Chirila | last post by:
Hi I have the following class: class PropTest: def __init__(self): self.prop1 = 1 self._prop2 = 2 self.__prop3 = 3
0
by: Gil Tal | last post by:
Hi, I use urllib2 to download a redirected url and I get an exception from the bowels of urllib2. It seems that urllib2 implements some super sophisticated self check and tries to control the...
2
by: Iyer, Prasad C | last post by:
Actually I am bit confused between the modules and .py file How do I differentiate between the 2. For example I have a file import1.py, import2.py file Which has few functions and classes And...
8
by: Øyvind Jægtnes | last post by:
I'm playing around a bit with XPath and nodelist and i want to extract some info from a RSS feed. The one that i am testing at can be viewed at http://slashdot.org/index.rss Ok.. heres the deal:...
8
by: Steven D'Aprano | last post by:
I came across this unexpected behaviour of getattr for new style classes. Example: >>> class Parrot(object): .... thing = .... >>> getattr(Parrot, "thing") is Parrot.thing True >>>...
7
by: Dylan Parry | last post by:
Hi, I've started to get a bit confused by namespaces and hierarchical organisation in general. What I want to do is something like this: Namespace MyNamespace | |- Class Foo | |- Namespace...
1
by: Giulio Petrucci | last post by:
Hi everybody, I'm getting confused about "which-name-give-to-what" developing my applications using Visual Studio. If I have to develop a "Utilities" library containing some classes for logging...
2
by: Giulio Petrucci | last post by:
Hi everybody, I'm getting confused about "which-name-give-to-what" developing my applications using Visual Studio. If I have to develop a "Utilities" library containing some classes for logging...
4
by: Emin | last post by:
Dear experts, I got some unexpected behavior in getattr and copy.deepcopy (see transcript below). I'm not sure if this is actually a bug in copy.deepcopy or if I'm doing something too magical...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.