473,467 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

__getattr__ equivalent for a module

Hi,

in any python class it is possible to define __getattr__ method so that if we try to get some value of not actually exists instance attribute, we can get some default value.

For example:

class MyClass:

def __getattr__(self, attname):

if attname.startswith('a'):
return "*"
i = MyClass()
....
i.aValue # it gives "*" if "i.aValue" will not be set before this call
i need to define the same behavior for a module:

import mymodule
mymodule.anyattribute
or
from mymodule import anyattribute

"anyattribute" is not actually defined in the module, but gives some attribute of the module

so my question is: how to tune up a module get default attribute if we try to get access to not actually exists attribute of a module?

(python 2.4 or 2.2)

many thanks for help.
--
Maksim Kasimov
Jan 15 '07 #1
2 2793
Maksim Kasimov wrote:
so my question is: how to tune up a module get default attribute if we
try to get access to not actually exists attribute of a module?
You could wrap it in an object, but that's a bit of a hack.

import sys

class Foo(object):
def __init__(self, wrapped):
self.wrapped = wrapped

def __getattr__(self, name):
try:
return getattr(self.wrapped, name)
except AttributeError:
return 'default'

sys.modules[__name__] = Foo(sys.modules[__name__])
Jan 15 '07 #2

Hi Leif, many thanks - it works

Leif K-Brooks wrote:
Maksim Kasimov wrote:
>so my question is: how to tune up a module get default attribute if we
try to get access to not actually exists attribute of a module?

You could wrap it in an object, but that's a bit of a hack.

import sys

class Foo(object):
def __init__(self, wrapped):
self.wrapped = wrapped

def __getattr__(self, name):
try:
return getattr(self.wrapped, name)
except AttributeError:
return 'default'

sys.modules[__name__] = Foo(sys.modules[__name__])

--
Maksim Kasimov
Jan 15 '07 #3

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

Similar topics

3
by: Greg Brunet | last post by:
In adding the ability to refer to field values using dbfFile.field notation, I learned how to use __getattr__ and __setattr__ . After some trial and error, I got it working. But as part of my...
1
by: Holger Joukl | last post by:
Hi there, please excuse my rather lengthy post. With introduction of the new style classes, something seems to have changed for __getattr__ hooks, even for classic classes: getattr.py: class...
0
by: Christian Hudon | last post by:
Hi, I'd like to be able to trap lookups of attributes of the current module. Exactly like what __getattr__ does, but for modules. As an exmaple, I'd like to be able to do: File foo.py ...
6
by: Erik Johnson | last post by:
Maybe I just don't know the right special function, but what I am wanting to do is write something akin to a __getattr__ function so that when you try to call an object method that doesn't exist,...
1
by: aum | last post by:
Hi, Does Javascript have any equivalent to Python's __getattr__ and __setattr__ methods? In other words, the option to define a method of a class that gets invoked whenever someone tries to...
5
by: glomde | last post by:
Hi, I tried to write a decorator for that should be for methods but for some reasons it doens seem to work when you try to do it on the __getattr__ method in a class. Could anybody give some...
13
by: ArseAssassin | last post by:
I'm using minidom to parse XML and to simplify accessing child nodes, I'm trying to implement __getattr__ for the Node class. Yes, I know what most of you will think of that; I'll just have to be...
4
by: Enrico | last post by:
Hi there, I have the following situation (I tryed to minimize the code to concentrate on the issue): def __getattr__(self, name): print 'A.__getattr__' if name == 'a': return 1 raise...
7
by: =?UTF-8?Q?Alexandru_Mo=C8=99oi?= | last post by:
i'm facing the following problem: class Base(object): def __getattr__(self, attr): return lambda x: attr + '_' + x def dec(callable): return lambda *args: 'dec_' + callable(*args) class...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.