Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do overridden methods inherit decorators in python?

Just like the title says, do overridden methods inherit decorators?

class A:     @memoized     def fun(self, arg):         return None   class B(A):     def fun(self, arg):         #computations         return something 

so does B.fun() maintain the decorator?

like image 256
Falmarri Avatar asked Dec 03 '10 22:12

Falmarri


People also ask

Is there an override decorator in Python?

If you want to add @override , the @override decorator can't actually do any override checking. You then have two options. Either there is no override checking, in which case @override is no better than a comment, or the type constructor needs to specifically know about @override and check it at class creation time.

How does override work in Python?

In Python method overriding occurs by simply defining in the child class a method with the same name of a method in the parent class. When you define a method in the object you make this latter able to satisfy that method call, so the implementations of its ancestors do not come in play.

Are decorators Pythonic?

Decorators are a very powerful and useful tool in Python since it allows programmers to modify the behaviour of a function or class. Decorators allow us to wrap another function in order to extend the behaviour of the wrapped function, without permanently modifying it.

Does Python support function overriding?

Method overriding in Python is when you have two methods with the same name that each perform different tasks. This is an important feature of inheritance in Python. In method overriding, the child class can change its functions that are defined by its ancestral classes.


2 Answers

Think about it this way

class A(object):     def fun(self, arg):         return None     fun = memoized(fun) 
like image 162
kevpie Avatar answered Oct 04 '22 05:10

kevpie


No. It's a completely different function. But you can try that for yourself with a dummy decorator.

like image 41
Gabi Purcaru Avatar answered Oct 04 '22 05:10

Gabi Purcaru