Is it possible to access the name of a superclass static method, when defining a subclass attribute, without specifically naming the super-
class?
Contrived example:
class SuperClass(object):
@staticmethod
def foo():
pass
class SubClass(SuperClass):
bar = SuperClass.foo
^^^^^^^^^^
Is there a way to do this without specifically naming 'SuperClass'?
Is it possible to access the name of a superclass static method, when >defining a subclass attribute, without specifically naming the super-
class?
Contrived example:
class SuperClass(object):
@staticmethod
def foo():
pass
class SubClass(SuperClass):
bar = SuperClass.foo
^^^^^^^^^^
Is there a way to do this without specifically naming 'SuperClass'?
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.
... bar = SubClass.fooclass SubClass(SuperClass):
... bar = self.fooclass SubClass(SuperClass):
You can define a classmethod in SubClass that seems to do the job:
class SuperClass(object):
@staticmethod
def spam(): # "spam" and "eggs" are a Python tradition
print('spam from SuperClass')
class SubClass(SuperClass):
@classmethod
def eggs(self):
super().spam()
SubClass.eggs() # Prints "spam from SuperClass"
On 11Nov2022 10:21, Ian Pilcher <arequipeno@gmail.com> wrote:
class SubClass(SuperClass):
bar = SuperClass.foo
^^^^^^^^^^
Is there a way to do this without specifically naming 'SuperClass'?
I think not.
Is it possible to access the name of a superclass static method, when >defining a subclass attribute, without specifically naming the super-
class?
Contrived example:
class SuperClass(object):
@staticmethod
def foo():
pass
class SubClass(SuperClass):
bar = SuperClass.foo
^^^^^^^^^^
Is there a way to do this without specifically naming 'SuperClass'?
On 11/11/22 11:02, Thomas Passin wrote:
You can define a classmethod in SubClass that seems to do the job:
class SuperClass(object):
@staticmethod
def spam(): # "spam" and "eggs" are a Python tradition
print('spam from SuperClass')
class SubClass(SuperClass):
@classmethod
def eggs(self):
super().spam()
SubClass.eggs() # Prints "spam from SuperClass"
That did it! Thanks!
Is it possible to access the name of a superclass static method, when defining a subclass attribute, without specifically naming the super-
class?
Contrived example:
class SuperClass(object):
@staticmethod
def foo():
pass
class SubClass(SuperClass):
bar = SuperClass.foo
^^^^^^^^^^
Is there a way to do this without specifically naming 'SuperClass'?
On 11/11/2022 16:21, Ian Pilcher wrote:
Is it possible to access the name of a superclass static method, when
defining a subclass attribute, without specifically naming the super-
class?
Contrived example:There is, but it's weird. I constructed classes from yaml config so I
class SuperClass(object):
@staticmethod
def foo():
pass
class SubClass(SuperClass):
bar = SuperClass.foo
^^^^^^^^^^
Is there a way to do this without specifically naming 'SuperClass'?
did not even know the name of super class but I wanted similar things
for my clabate templates and I implemented superattr() which works for me:
class BasePage:
body = '<p>Hello</p>'
class MySpecificPage(BasePage):
body = superattr() + '<p>World<p/>'
Actually, it's suboptimally elegant code. Artistic code, to be clear, as
if you looked at modern art and thought: WTF? Also, it's specific for templates only and supports only __add__.
But I think the approach can be extended to a general superclass() if
you log __getattr__ calls and apply them in __get__ method same way.
I realize this reply is not an immediate help and probably won't help,
but there's always a way out.
Axy.
Here's the code:
class superattr:
'''
This is a descriptor that allows extending attributes in a simple and
elegant way:
my_attr = superattr() + some_addition_to_my_attr
'''
def __init__(self):
self.additions = []
def __set_name__(self, owner, name):
print('__set_name__', name)
self.attr_name = name
def __get__(self, obj, objtype=None):
for cls in obj.__class__.__mro__[1:]:
try:
value = getattr(cls, self.attr_name)
except AttributeError:
continue
for a in self.additions:
value = value + a
return value
raise AttributeError(self.attr_name)
def __add__(self, other):
print('__add__:', other)
self.additions.append(other)
return self Full article: https://declassed.art/en/blog/2022/07/02/a-note-on-multiple-inheritance-in-python
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 31:38:06 |
Calls: | 10,391 |
Calls today: | 2 |
Files: | 14,064 |
Messages: | 6,417,109 |