Skip to content Skip to sidebar Skip to footer

How To Get Self Object Name From Self Method In Python

I am trying to find a way to automatically print the object reference name with just a print object To be more specific. Lets say I have a class: class A: def __init__(self):

Solution 1:

As the comments on your question have stated, it is not possible and also unwise, consider something along the lines of the following approach instead:

class A:
    def __init__(self, name):
        self.cards = []
        self.name = name

    def __str__(self):
        return '{} contains ...'.format(self.name)

>>> test = A('test')
>>> print test
test contains ...

>>> a = A('hello')
>>> print a
hello contains ...

Post a Comment for "How To Get Self Object Name From Self Method In Python"