Учитывая класс test
, почему невозможно создать его экземпляр, вызвав один из его методов вместе с конструктором?
class test:
def __init__(self, a):
self.a = a
def print_a(self):
print(self.a)
Вот пример:
>>> obj = test("Hello").print_a() # Prints the desired output.
Hello
>>> obj
>>> print(obj) # But the object does not exist.
None
>>> obj = test("Hello") # It obviously works when doing it separately.
>>> obj
<__main__.test object at 0x7f537fea3940>
>>> obj.print_a()
Hello
Почему невозможно связать вызов метода с вызовом конструктора?
Это было реализовано в python3
.