Python decorator which set class variable?

Doing this directly with a function decorator is impossible, since you would need to access the class which is currently being defined, and this class does not exist yet. One solution would be to make the decorator just store the regex as an attribute of the method and have functionality collecting these methods on a base class: def notify(regex): def decorate(func): func. Regex = regex return func return decorate class Baz(object): @property def function_list(self): for attr in dir(self): obj = getattr(self, attr) if callable(obj) and hasattr(obj, "regex"): yield obj class FooBar(Baz): @notify(".

*") def everything(self, message): pass @notify("(\w+):. *") def reply(self, message): pass for foo in FooBar(). Function_list: print("%s => %s" % (foo.

Regex, foo)).

Heh, we both posted similar solutions just seconds apart. The property is a nice idea, I didn't think of that. – Jochen Ritzel Mar 17 at 18:10 @Jochen: Well, except for the timing that's not that much of a coincidence.

There are basically three options to do this: via a base class, via a class decorator or via metaclasses. Our solutions reflect the two easiest options out of those three :) – Sven Marnach Mar 17 at 18:15.

When notify is called the class Foobar does not even exist yet. Therefore, you can't do with just the decorator. What you can do is mark the functions with a decorator and collect them after the class has been defined.

You can do it with a metaclass or with a class decorator like this: import inspect def notify(regex): def mark( func ): func. Regex = regex return func return mark def collect( cls ): cls. FunctionList= for name, func in inspect.

Getmembers(cls, inspect. Ismethod): if hasattr(func, 'regex'): cls.functionList. Append(func) return cls @collect class FooBar(object): @notify(".

*") def everything(self, message): pass @notify("(\w+):. *") def reply(self, message): pass for foo in FooBar. FunctionList: print("%s => %s" % (foo.

Regex, foo)).

I wrote it anyways so I might was well just post the third option. It's using a metaclass to collect the functions: def notify(regex): def mark( func ): func. Regex = regex return func return mark class RegexBase(object): class __metaclass__(type): """ creates a list of functions with a `regex` attribute and stores it on the class as `functionList` """ def __new__(cls, name, bases, attr): fl = for obj in attr.itervalues(): if hasattr(obj, 'regex'): fl.

Append(obj) attr'functionList' = fl return type. __new__(cls, name, bases, attr) class FooBar(RegexBase): @notify(". *") def everything(self, message): pass @notify("(\w+):.

*") def reply(self, message): pass for foo in FooBar. FunctionList: print("%s => %s" % (foo. Regex, foo)) I guess it's nice to have all the options together in one thread.

So that's the third option. Nice! :) – Sven Marnach Mar 17 at 18:58.

Doing this directly with a function decorator is impossible, since you would need to access the class which is currently being defined, and this class does not exist yet. For foo in FooBar(). Print("%s => %s" % (foo.

When notify is called the class Foobar does not even exist yet. Therefore, you can't do with just the decorator. What you can do is mark the functions with a decorator and collect them after the class has been defined.

For name, func in inspect. For foo in FooBar.

Green eyes are caused by the genes for eye color you inherited from your parents. Genes determine which pigments will be shown in your irises. The genetics involved in eye color are highly complex.

You can find more information here: thetech.org/genetics/ask.php?id=29.

As in the case of blue eyes, the color of green eyes does not result simply from the pigmentation of the iris. Rather, its appearance is caused by the combination of an amber or light brown pigmentation of the stroma, given by a low or moderate concentration of melanin, with the blue tone imparted by the Rayleigh scattering of the reflected light. Green eyes probably result from the interaction of multiple variants within the OCA2 and other genes.

They were present in south Siberia during the Bronze Age. 47 They are most common in Northern and Central Europe.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions