Answers for "python print a class"

4

print class python

class yourClass:
    def __str__(self):
        return "value"  #replace value with what you want to print
Posted by: Guest on June-10-2021
8

override python print for class

>>> class Test:
...     def __repr__(self):
...         return "Test()"
...     def __str__(self):
...         return "member of Test"
... 
>>> t = Test()
>>> t
Test()
>>> print(t)
member of Test
Posted by: Guest on August-15-2020
0

python print class variables

# to print out the variable names
print(vars(YourClass))

# to print out names and values
for var in vars(YourClass):
    print(getattr(YourClass, var))
Posted by: Guest on March-06-2020

Python Answers by Framework

Browse Popular Code Answers by Language