0

I am making a function called makeTrigger and it takes a in variables and updates a dictionary but the thing I have a problem with is that I am trying to access a obj in a dict and I am getting this with str():

<ps6.SubjectTrigger object at 0x101d36fd0> and <ps6.TitleTrigger object at 0x101dbc0d0>

What will make it print this:

SubjectTrigger('world') and TitleTrigger('Intel')

What should I use to print this correctly. Here is the dict I am trying to access:

{'t1': SubjectTrigger('world'), 't2': TitleTrigger('Intel'), 't3': PhraseTrigger('New York City')}

I am trying to acces t1 and t2.

Adam
  • 15,537
  • 2
  • 42
  • 63
  • 2
    It sounds like you're asking how to change the text representation of your object, which has been explained [in this answer](http://stackoverflow.com/a/1535336/1106367). – Adam Mar 31 '13 at 20:47

1 Answers1

1

this question is surely a duplicate.
–––––––

to change the representation of an object you have to make a method in the class definition of the object

class MyClass:

    def __repr__(self):
        return "Hello ,this is a test"

the

__repr__

method returns a string that will be used by the print function to represent the object, so if you create a MyClass object and then you print it the output will be

Hello ,this is a test

Note:
this method doesn't implement str(MyClass()) ; to do that you have to make another method called

__str__
Alberto Perrella
  • 1,318
  • 5
  • 12
  • 19