Superclass static method name from subclass
Ian Pilcher
arequipeno at gmail.com
Fri Nov 11 17:04:34 EST 2022
On 11/11/22 11:29, Dieter Maurer wrote:
> Ian Pilcher wrote at 2022-11-11 10:21 -0600:
>>
>> class SuperClass(object):
>> @staticmethod
>> def foo():
>> pass
>>
>> class SubClass(SuperClass):
>> bar = SuperClass.foo
>> ^^^^^^^^^^
>>
>> Is there a way to do this without specifically naming 'SuperClass'?
>
> Unless you overrode it, you can use `self.foo` or `SubClass.foo`;
> if you overrode it (and you are using either Python 3 or
> Python 2 and a so called "new style class"), you can use `super`.
> When you use `super` outside a method definition, you must
> call it with parameters.
SubClass.foo doesn't work, because 'SubClass' doesn't actually exist
until the class is defined.
>>> class SubClass(SuperClass):
... bar = SubClass.foo
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in SubClass
NameError: name 'SubClass' is not defined. Did you mean: 'SuperClass'?
Similarly, self.foo doesn't work, because self isn't defined:
>>> class SubClass(SuperClass):
... bar = self.foo
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in SubClass
NameError: name 'self' is not defined
--
========================================================================
Google Where SkyNet meets Idiocracy
========================================================================
More information about the Python-list
mailing list