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

#The repr() function returns a printable representation of the given object.
#repr() takes a single object.
#Syntax
val = "string"
print(repr(val)) #output ---->"'string'"
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 :: python logging variables extra 
Python :: filter query objects by date range in Django 
Python :: sklearn labelbinarizer in pipeline 
Python :: get hex code of character python 
Python :: Python how to search in string 
Python :: Math Module exp() Function in python 
Python :: Subset data frame by date 
Python :: string to float in python 
Python :: how to extract dictionary value from string in python 
Python :: destructuring for dict in python 
Python :: python insert item into list 
Python :: getting tradingview historical data using python 
Python :: anaconda python 3.6 download 
Python :: tkinter canvas text 
Python :: print only strings in list python 
Python :: what is attribute in python 
Python :: how to join basename and directory in python os 
Python :: sns boxplot ylabelsize 
Python :: dict ;get a key of a value 
Python :: gurobi get feasible solution when timelimit reached 
Python :: Using Python-docx to update cell content of a table 
Python :: install nsml python 
Python :: python convert xml to dictionary 
Python :: unlimited arguments 
Python :: add colorbar without changing subplot size 
Python :: groupby in python 
Python :: img not responding jupyter notebook imshow 
Python :: pandas redondear un valor 
Python :: spacy shortforms explanation 
Python :: Access python http.server on google colab 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =