On 08/10/2025 1:24, Vincent Macri wrote:
class A:
     def foo(self):
         return 5
     bar = foo

a = A()

a.__getattribute__('foo').__name__ == 'foo'  # True

a.__getattribute__('bar').__name__ == 'bar'  # False, because a.__getattribute__('bar').__name__ is 'foo'

So you can distinguish an alias like this. (Both 'foo' and 'bar' are in `a.__dir__()` by default, so iterate over each `s` in the super `__dir__` and check if `self.__getattribute__(s).__name__ == s`.)
When handling inheritance, there is a disadvantage to defining an alias with `bar = foo`. Using the same class A as above, the following may be surprising:

sage: class B(A):
....:     def foo(self):
....:         return 42
....:
sage: b = B()
sage: b.foo()
42
sage: b.bar() # surprise?
5
sage: b.__getattribute__("bar").__name__
'foo'
sage: b.__getattribute__("foo").__name__
'foo'

In some places in Sage an alias `baz` will be defined as

def baz(self):
    return self.foo()

and a short doc that says it is an alias, instead of the better solution of having the full docstring of foo() with a prefix that tells this is an alias.

Is there any idiom (or even a common library) to define aliases that works for functions and methods, in python and in cython, with sensible handling of docstrings and inheritance?

Regards,
TB

--
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/sage-devel/b40b72ee-6537-4af2-a560-cda7f9c87cc8%40gmail.com.

Reply via email to