lundi 9 novembre 2020

Is it possible to have a method that returns a reference to itself in python? [closed]

I was wondering if it is possible to have method return a reference to itself. (don't ask why.) I've tried to use a class for self and get reflection there but no dice...

>>> class test:
...     def ex(self):
...             return(self.ex)
...
>>> t = test()
>>> a = t.ex()
>>> b = t.ex
>>> a==b
True
>>> a()
<bound method test.ex of <__main__.test object at 0x033189F0>>
>>> b()
<bound method test.ex of <__main__.test object at 0x033189F0>>
>>> a(t)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ex() takes 1 positional argument but 2 were given
>>> b(t)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ex() takes 1 positional argument but 2 were given

And I also tried with just a def but obviously I had nothing to return...

>>> def test():
...     return(self)
...
>>> test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in test
NameError: name 'self' is not defined

Finally I typed the word lambda into the terminal and then just said 'yeah, that won't help either huh... lulz'.





Aucun commentaire:

Enregistrer un commentaire