4

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):
        self.cards = []

    def __str__(self):
        # return a string representation of A
        return "A contains " ...
    ...

Now whenever i create an object

test = A()

and I use the print test it will get something like (do not mind the dots)

A contains ...

What I want to achieve is to automatically print the object reference name instead of the class name:

test contains ...

The self.__class__ or self.__name__ wont work since it returns a weird string like <class '__main__.A'>.

How should __str__ be implemented to achieve this? Thanks in advance.

6
  • 1
    Objects don't have names, they just have references. Commented May 10, 2014 at 11:08
  • 1
    are you trying to print the identifier of the variable your instance is bound to? if so, i don't think thats possible ( or wise ). consider an instance of class A that is not bound to any variable. what would you print then? or if it is bound to two or many variables? maybe you could pass a name for that object in the constructor and print that. Commented May 10, 2014 at 11:08
  • What are you trying to accomplish? Commented May 10, 2014 at 11:27
  • @Daenyth Well each time i use a print object to just print itself and not its class Commented May 10, 2014 at 11:32
  • Why are you trying to print the object? What goal are you trying to accomplish by that Commented May 10, 2014 at 12:57

2 Answers 2

2

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 ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply. That is exactly what I want to avoid, passing the reference name on instantiation. If this is the only solution then I guess what I asked is not possible :|
1

I know I am replying too late but if anyone stumbles here like I do here is the solution:

type(self).__name__

your implementation would be:

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

    def __str__(self):
        return '{} contains...'.format(type(self).__name__)
a=A('test')
print(a)
# A contains ...

1 Comment

This is not what the asker wanted. The desired output in this case is "test contains ...", not "A contains..."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.