Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python __repr__

# 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))
Comment

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)+ ')'
Comment

__repr__ in python

    def __repr__(self) -> str:
        return
Comment

python __repr__ meaning

>>>x=4
>>>repr(x)
'4'
>>>str(x)
'4'
>>>y='stringy'
>>>repr(y)
"'stringy'"
>>>str(y)
'stringy'
Comment

python __repr__

import datetime
now = datetime.datetime.now()
now.__str__()
#>>> '2020-12-27 22:28:00.324317'
now.__repr__()
#>>> 'datetime.datetime(2020, 12, 27, 22, 28, 0, 324317)'
Comment

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]
Comment

python __repr__ meaning

>>>repr(y)
"'a string'"
>>>y2=eval(repr(y))
>>>y==y2
True
Comment

PREVIOUS NEXT
Code Example
Python :: pthon return value with highest occurences 
Python :: remove all elements from list python by value 
Python :: ploting bargraph with value_counts 
Python :: name, *line = input().split() 
Python :: find average with sum and len in python 
Python :: hex to string python 
Python :: how to add list as new row to pandas dataframe 
Python :: python how to check in a list 
Python :: how to remove a letter from a string python 
Python :: re.match python 
Python :: balancing paranthesis python 
Python :: how to take out every even number from a list in python 
Python :: how to initialize set in python 
Python :: how to use assert in python 
Python :: get operator as input in python 
Python :: default values python 
Python :: python bytes to string 
Python :: class indexing 
Python :: python round 1 decimal place 
Python :: group by dateime pandas 
Python :: how to add labels on bar of barchart seaborn 
Python :: python 3 tkinter treeview example 
Python :: add horizontal line to plotly scatter 
Python :: staticmethod python 
Python :: docker opencv python libGL.so.1: cannot open shared object file: No such file or directory 
Python :: how to sort a list in python 
Python :: getting size of list in python 
Python :: python factor number 
Python :: tensorflow.keras.utils.to_categorical 
Python :: Converting objects into integers in python 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =