Decoratros, class - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Decoratros, class (/thread-26105.html) |
Decoratros, class - niski1996 - Apr-21-2020 Hi, I've got code like that: def check_str_or_int(fun): def inside_check(arg, obj, *tags): if type(arg)==int: fun(arg, obj, *tags) elif type(arg)==str: fun(obj.get_num_by_name(arg), obj,*tags) return inside_check class PrzestrzenObiektow(): ... @check_str_or_int def add_tags(self, arg1, arg2, *tags): pass from Krzykacz import przestrzen_obiektow def setup_module(): przestrzen = przestrzen_obiektow.PrzestrzenObiektow() przestrzen.dodawanie_do_przestrzeni(None, 'a', magnet_points={'konce': [(10, 10), (300, 700)], 'srodki': [(110, 1000)]}) przestrzen.dodawanie_do_przestrzeni(None, 'b', magnet_points={'konce': [(200, 10), (89, 90), (90, 89)], 'srodki': [(110, 110)]}) przestrzen.add_tags() setup_module()When I call przestrzen.add_tags(), I expect: instead I get: in debugger 'arg' is <Krzykacz.przestrzen_obiektow.PrzestrzenObiektow object at 0x00000197241BCB08> , witch is PrzestrzenObiektow instance. It looks like 'self' is pass to the decorator. How to fix it? I want to use these decorator with method and funtion. Could you pls help?
RE: Decoratros, class - deanhystad - Apr-21-2020 The inner function has to have the same arguments as the function it decorates. Normally this is done by using *args and **kwargs which match any arguments, but in your case your inside_check function needs a "self" argument. It doesn't have to use it, but it needs it so it can provide that as an argument and make the signature match. |