Defining a decorator in a class, which is also usuable within the class definition?

There are two solutions to this problem that I can think of. The simplest is to make it a static method after you're done using it in the parent class: class Command(object): def subcommand(method): # Regular function in class definition scope. Method.

Is_subcommand = True return method @subcommand def common(self): print "this subcommand is available to all child classes" subcommand = staticmethod(subcommand) # Now a static method. Can no longer be called during class definition phase This is somewhat fragile in that you can't use it in the parent class after you make it a static method. The more robust way to do this is to add an intermediate class: class Command(object): @staticmethod def subcommand(method): method.

Is_subcommand = True return method class CommandBase(Command): @Command. Subcommand def common(self): print "this subcommand is available to all child classes You can now inherit all of your classes from CommandBase instead of Command.

There are two solutions to this problem that I can think of. The simplest is to make it a static method after you're done using it in the parent class: class Command(object): def subcommand(method): # Regular function in class definition scope. Method.

Is_subcommand = True return method @subcommand def common(self): print "this subcommand is available to all child classes" subcommand = staticmethod(subcommand) # Now a static method. Can no longer be called during class definition phase. This is somewhat fragile in that you can't use it in the parent class after you make it a static method.

The more robust way to do this is to add an intermediate class: class Command(object): @staticmethod def subcommand(method): method. Is_subcommand = True return method class CommandBase(Command): @Command. Subcommand def common(self): print "this subcommand is available to all child classes" You can now inherit all of your classes from CommandBase instead of Command.

This is sort of surprising. I would have thought that when executing decorators of class methods the class itself wasn't existing yet (the metaclass is called at the end), but still subcommand is found. Anyway both your examples have typos: missing end quote in first and probably you meant @Command.

Subcommand in second. Am I correct? – 6502 Jan 30 at 21:54 @6502 I've fixed the typos.

If you look at the definition of subcommand in the first example, you'll see that it's defined as a regular function. All of the method definitions take place in the scope in which subcommand is found so it's to be expected that subcommand is visible in that scope. – aaronasterling Jan 30 at 21:58 Yes it makes sense... subcommand is just a local variable until the end of the body of the class, at that point the dictionary is built and sent to metaclass.

Still I feel strange writing e.g. A while loop in the body of a class statement :-D ... – 6502 Jan 30 at 22:10 — Your second suggestion is interesting. Currently, I'm conflating the functionality of the base class (the default subcommands) and the subcommand feature itself. If I move just the subcommand methods (__call__ and subcommand) into a dedicated class, I think it will be quite elegant.

Thank you! – Ben Blank Jan 30 at 22:30.

This is somewhat fragile in that you can't use it in the parent class after you make it a static method. You can now inherit all of your classes from CommandBase instead of Command.

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