473,387 Members | 1,606 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,387 software developers and data experts.

accepts decorator

i want to make a decorator that make a function accept only types,if
the types are wrong but the number of types is equal to arguments try
to convert them to the type.

for example:

@Accept(int,int)
def Sum(a,b):
return a+b

print Sum("2",2.0)

this should print 4,coz the decorator sees that there is the same
amount of arguments in Accept and Sum,then it will try to cast them to
the types(a to int and also b to int),then he will send them to the
normal function so Sum will get a=2,b=2 instead of a="2"(string)
b=2.0(float).
i made this but it doesn`t work:
def Accept(*types):
def check_accepts(f):
assert len(types) == f.func_code.co_argcount-1,"Not
Enougth/Too much Types"
Types=types
def new_f(*args, **kwds):
newArgs=[]
for (a, t) in zip(kwds.values(), Types):
try:
newArgs.append(t(a))
except:
raise Exception("Convertion of %s to type %s
failed" %(a,t))
nargs=tuple(newArgs)
return f(*nargs, **kwds)
new_f.func_name = f.func_name
return new_f
return check_accepts

it doesn`t work when i decorate a instance method.
i get in *args the instance and in **kwds the arguments,but if it is
just a function not a instance method i get in *args the arguments and
in **kwds a empty dict.

can someone also explain me what is * and ** ,and how they are called.

Thanks,
-Uriel Katz

Sep 25 '06 #1
3 1652
i think i got what the * and ** mean.
*args mean arguments that are assigned by position(that is why *arg is
a tuple)
**kwds mean arguments that are assigned using equals a=2,b=3 (that is
why **kwds i a dict)
but why self is in *args and other thing goes to **kwds? even if not
assigned using equals

Sep 26 '06 #2
At Monday 25/9/2006 21:58, urielka wrote:
>i think i got what the * and ** mean.
*args mean arguments that are assigned by position(that is why *arg is
a tuple)
**kwds mean arguments that are assigned using equals a=2,b=3 (that is
why **kwds i a dict)
but why self is in *args and other thing goes to **kwds? even if not
assigned using equals
For these elemental things you should start by reading the Python
tutorial included with the documentation - it's easy.
And for decorators, this is a good text
http://www.phyast.pitt.edu/~micheles...mentation.html which
supplements the Python docs.

Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Sep 26 '06 #3
hehe i saw that,that is what made my understand it.
the decorator now works.
let say i have a function decorated with two decorators:

@Accept(int,int)
@OtherDecorator
def myfunc(a,b):
pass

how can i make the two decorators into one(note:one get parameters and
the other doesn`t)

Sep 26 '06 #4

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

Similar topics

30
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...
22
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...
5
by: Doug | last post by:
I am looking at using the decorator pattern to create a rudimentary stored proc generator but am unsure about something. For each class that identifies a part of the stored proc, what if I want to...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.