“__str __ ()” Ответ

__str __ ()

>>> class Test:
...     def __repr__(self):
...         return "Test()"
...     def __str__(self):
...         return "member of Test"
... 
>>> t = Test()
>>> t
Test()
>>> print(t)
member of Test
Arrogant Alpaca

python __repr__


class Person:
    name = ""
    age = 0

    def __init__(self, personName, personAge):
        self.name = personName
        self.age = personAge

    def __repr__(self):
        return {'name':self.name, 'age':self.age}

    def __str__(self):
        return 'Person(name='+self.name+', age='+str(self.age)+ ')'
rebellion

__str__python

The __str__ method in Python represents the class objects as a string – it can be used for classes. 
This method is called when print() or str() function is invoked on an object. This method must return the String object.
Red Team

Ответы похожие на “__str __ ()”

Смотреть популярные ответы по языку

Смотреть другие языки программирования