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 hints why this is?
Example:
class decoratorTest(o bject):
def __init__(self, func):
self.func = func
def __get__(self, instance, cls=None):
print "__get__", instance
self.instance = instance
return self
def __call__(self, *args, **kwds):
return self.func(self. instance, *args, **kwds)
class MyClass1(object ):
@decoratorTest
def decoratorTestFu nc(self):
print "hello1"
@decoratorTest
def __getattr__(sel f,name):
print "hello2"
a = MyClass1()
a.decoratorTest Func() # This works since the __get__ method is called
and the instance is retreived
a.test # This doesnt call the __get__ !!!
Output
__get__ <__main__.MyCla ss1 object at 0x4012baac>
hello1
Traceback (most recent call last):
File "/home/tonib/test.py", line 27, in ?
a.test
File "/home/tonib/test.py", line 12, in __call__
return self.func(self. instance, *args, **kwds)
AttributeError: 'decoratorTest' object has no attribute 'instance' 5 2411
glomde <tb****@yahoo.c omwrote:
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 hints why this is?
....
a.test # This doesnt call the __get__ !!!
Output
__get__ <__main__.MyCla ss1 object at 0x4012baac>
hello1
Traceback (most recent call last):
File "/home/tonib/test.py", line 27, in ?
a.test
File "/home/tonib/test.py", line 12, in __call__
return self.func(self. instance, *args, **kwds)
AttributeError: 'decoratorTest' object has no attribute 'instance'
What Python release are you using? With Python 2.5, your code gives me
instead:
>>a.test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "a.py", line 11, in __call__
return self.func(self. instance, *args, **kwds)
TypeError: __getattr__() takes exactly 2 arguments (3 given)
>>>
so there would seem to be some "mis-alignment" wrt the problems you
observe...
Alex
On Mar 28, 4:47 pm, a...@mac.com (Alex Martelli) wrote:
glomde <tbr...@yahoo.c omwrote:
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 hints why this is?
...
a.test # This doesnt call the __get__ !!!
Output
__get__ <__main__.MyCla ss1 object at 0x4012baac>
hello1
Traceback (most recent call last):
File "/home/tonib/test.py", line 27, in ?
a.test
File "/home/tonib/test.py", line 12, in __call__
return self.func(self. instance, *args, **kwds)
AttributeError: 'decoratorTest' object has no attribute 'instance'
What Python release are you using? With Python 2.5, your code gives me
instead:
>a.test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "a.py", line 11, in __call__
return self.func(self. instance, *args, **kwds)
TypeError: __getattr__() takes exactly 2 arguments (3 given)
so there would seem to be some "mis-alignment" wrt the problems you
observe...
Alex
I was using 2.4 but I downloaded and installed ActivePython2.5 .0.0 and
for I get the same message as in my original email.
What python version do you use more exactly?
/T
On Mar 28, 8:28 am, "glomde" <tbr...@yahoo.c omwrote:
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 hints why this is?
All you have to do is decorator should methods write be for reason it
works with class __getattr__ in!
On Mar 28, 3:47 pm, a...@mac.com (Alex Martelli) wrote:
glomde <tbr...@yahoo.c omwrote:
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 hints why this is?
...
a.test # This doesnt call the __get__ !!!
Output
__get__ <__main__.MyCla ss1 object at 0x4012baac>
hello1
Traceback (most recent call last):
File "/home/tonib/test.py", line 27, in ?
a.test
File "/home/tonib/test.py", line 12, in __call__
return self.func(self. instance, *args, **kwds)
AttributeError: 'decoratorTest' object has no attribute 'instance'
What Python release are you using? With Python 2.5, your code gives me
instead:
>a.test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "a.py", line 11, in __call__
return self.func(self. instance, *args, **kwds)
TypeError: __getattr__() takes exactly 2 arguments (3 given)
so there would seem to be some "mis-alignment" wrt the problems you
observe...
I get this error with python 2.4 when I do
a.__getattr__(' test') # This sets the 'instance' attribute as __get__
is called
a.test # __get__ is not called but 'instance' is set
To get python to run the __get__ method I think you have to call
__getattr__ explicitly:
a.__getattr__(' test')
If you do:
a.test
python follows a different routine: it checks for the existence of the
attribute, then check if there is a __getattr__ attribute. Now the
speculative bit: then I conjecture that python assumes that
__getattr__ is a function with two arguments and directly passes them
on to it. Indeed
type(a).__dict_ _['__getattr__'](a, 'test')
seems to produce the same errors as a.test, whether the instance
attribute is set or not.
And this explain why there are too many arguments (error message
above).
--
Arnaud
To get python to run the __get__ method I think you have to call
__getattr__ explicitly:
a.__getattr__(' test')
If you do:
a.test
python follows a different routine: it checks for the existence of the
attribute, then check if there is a __getattr__ attribute. Now the
speculative bit: then I conjecture that python assumes that
__getattr__ is a function with two arguments and directly passes them
on to it. Indeed
type(a).__dict_ _['__getattr__'](a, 'test')
seems to produce the same errors as a.test, whether the instance
attribute is set or not.
And this explain why there are too many arguments (error message
above).
--
Arnaud
So is this a python bug? I assumed it was seen as function but dont
understand why it is like this. But interesting that if you call
__getattr__ explicitly it works.
Intuitevely I would assumet that the same route should be followed
in both case.
Maybe it is because __getattr__ is called only when you have an
exception.
/T This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 trials, I added
some print statements to debug stuff. The ones I added to __setattr__
work as expected, but the one in __getattr__ seems to get called just
under 1000 times for every __getattr__ call!
Something is obviously not right here - but...
|
by: Ron_Adam |
last post by:
I was having some difficulty figuring out just what was going on with
decorators. So after a considerable amount of experimenting I was
able to take one apart in a way. It required me to take a closer look
at function def's and call's, which is something I tend to take for
granted.
I'm not sure this is 100%, or if there are other ways to view it, but
it seems to make sense when viewed this way.
Is there a way to do this same thing...
|
by: Ron_Adam |
last post by:
Hi, Thanks again for all the helping me understand the details of
decorators.
I put together a class to create decorators that could make them a lot
easier to use.
It still has a few glitches in it that needs to be addressed.
(1) The test for the 'function' object needs to not test for a string
but an object type instead.
|
by: Pelmen |
last post by:
How can I get rid of recursive call __getattr__ inside this method, if
i need to use method or property of the class?
|
by: Lad |
last post by:
I use Python 2.3.
I have heard about decorators in Python 2.4.
What is the decorator useful for?
Thanks for reply
L.
| |
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, it get's intercepted *along with
it's argument*, in the same manner as __getattr__ intercepts attributes
references for attributes that don't exist.
This doesn't quite work:
>>> class Foo:
|
by: thomas.karolski |
last post by:
Hi,
I would like to create a Decorator metaclass, which automatically
turns a class which inherits from the "Decorator" type into a
decorator.
A decorator in this case, is simply a class which has all of its
decorator implementation inside a decorator() method. Every other
attribute access is being proxied to decorator().getParent().
Here's my attempt:
-------------------------------------------------------
|
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 AttributeError('%s not found in A' % name)
def __getattr__(self, name):
|
by: mwojc |
last post by:
Hi!
My class with implemented __getattr__ and __setattr__ methods cannot be
pickled because of the Error:
======================================================================
ERROR: testPickle (__main__.TestDeffnet2WithBiases)
----------------------------------------------------------------------
Traceback (most recent call last):
File "deffnet.py", line 246, in testPickle
cPickle.dump(self.denet, file)
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |