“Python __reptr__” Code-Antworten

Python __reptr__


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

Python repr ()

numbers = [1, 2, 3, 4, 5]

# create a printable representation of the list
printable_numbers = repr(numbers)

print(printable_numbers)

# Output: [1, 2, 3, 4, 5]
Good Gentoo

Python __reptr__

# A simple Person class

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __repr__(self):
        rep = 'Person(' + self.name + ',' + str(self.age) + ')'
        return rep


# Let's make a Person object and print the results of repr()

person = Person("John", 20)
print(repr(person))
Charles Xu

Ähnliche Antworten wie “Python __reptr__”

Fragen ähnlich wie “Python __reptr__”

Weitere verwandte Antworten zu “Python __reptr__” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen