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

strange behaviour with decorators.


I tried the following program:

def positive(f):

def call(self, u):

if u < 1:
print 'Not Positive'
raise ValueError
return f(self, u)

return call

class Incrementor:

def __init__(self, val=0):
self.value = val

@positive
def __call__(self, term = 1):
print 'incrementing'
self.value += term
return self.value

inc = Incrementor(0)
try:
print inc(1)
print inc()
print inc(3)
print inc(-2)
except:
pass
And it gave me the following result:

incrementing
1
Now although this behaviour was surprising after somethought
I think I may understand why things go wrong, but I certainly
don't understand the result I got. I would think an error like:

TypeError: call() takes exactly 2 arguments (1 given)

would have been more appropiate.
Am I missing something?
Is it a bug?
Maybe both?

--
Antoon Pardon
Jul 18 '05 #1
2 1059
> Now although this behaviour was surprising after somethought
I think I may understand why things go wrong, but I certainly
don't understand the result I got. I would think an error like:

TypeError: call() takes exactly 2 arguments (1 given)

would have been more appropiate.
Am I missing something?
Is it a bug?
Maybe both?


Maybe I'm missing something - but I exactly get that error if I don't use
that try: except: of yours:

For this

-----------------
def positive(f):
def call(self, u):

if u < 1:
print 'Not Positive'
raise ValueError
return f(self, u)

return call

class Incrementor:
def __init__(self, val=0):
self.value = val

@positive
def __call__(self, term = 1):
print 'incrementing'
self.value += term
return self.value

inc = Incrementor(0)

print inc(1)
print inc()
print inc(3)

--------------------

I get this result:
deets@kumquat:~$ python2.4 /tmp/test.py
incrementing
1
Traceback (most recent call last):
File "/tmp/test.py", line 24, in ?
print inc()
TypeError: call() takes exactly 2 arguments (1 give

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
Op 2005-02-09, Diez B. Roggisch schreef <de*********@web.de>:
Now although this behaviour was surprising after somethought
I think I may understand why things go wrong, but I certainly
don't understand the result I got. I would think an error like:

TypeError: call() takes exactly 2 arguments (1 given)

would have been more appropiate.
Am I missing something?
Is it a bug?
Maybe both?


Maybe I'm missing something - but I exactly get that error if I don't use
that try: except: of yours:


Ah, yes, the penny dropped. The try: except where there because
originally there were other statements I wanted to test and I
didn't want the raise exception by the inc(-2) stop the script.
I completely forget to consider it would also catch the
error I was expecting.

Thanks.

--
Antoon Pardon
Jul 18 '05 #3

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

Similar topics

4
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it...
17
by: daishi | last post by:
For what it's worth: As far as I know, the proposed @decorator syntax will be the first time that two logical lines of python with the same indentation will not be independent of one another....
4
by: RebelGeekz | last post by:
Just my humble opinion: def bar(low,high): meta: accepts(int,int) returns(float) #more code Use a metadata section, no need to introduce new messy symbols, or mangling our beloved visual...
2
by: Guido van Rossum | last post by:
Robert and Python-dev, I've read the J2 proposal up and down several times, pondered all the issues, and slept on it for a night, and I still don't like it enough to accept it. The only reason...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
24
by: brian.bird | last post by:
Can anyone explain the behaviour of python when running this script? >>> def method(n, bits=): .... bits.append(n) .... print bits .... >>> method(1) >>> method(2)
9
by: Christian Eder | last post by:
Hi, I think I have discovered a problem in context of metaclasses and multiple inheritance in python 2.4, which I could finally reduce to a simple example: Look at following code: class...
2
by: Andrew West | last post by:
Probably a bit of weird question. I realise decorators shouldn't be executed until the function they are defined with are called, but is there anyway for me to find all the decorates declared in a...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...

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.