accepts decorator

urielka uriel.katz at
Tue Sep 26 00:09:21 CEST 2006

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:

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)
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"
            def new_f(*args, **kwds):
                for (a, t) in zip(kwds.values(), Types):
                        raise Exception("Convertion of %s to type %s
failed" %(a,t))
                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.

    -Uriel Katz

More information about the Python-list mailing list