Trying to Print values but getting memory location instead

后端 未结 1 666
盖世英雄少女心
盖世英雄少女心 2021-01-22 19:09

I have a function below which I want to output lines of values relating to \'O\', instead it prints the location of these values, how do I amend this? allReactions is an empty a

1条回答
  •  -上瘾入骨i
    2021-01-22 20:06

    You are trying to print a list of Reaction objects. By default, python prints a class object's ID because it really doesn't have much to say about it. If you have control over the class definition, you can change that by adding __str__ and __repr__ method to the class.

    >>> class C(object):
    ...     pass
    ... 
    >>> print C()
    <__main__.C object at 0x7fbe3af3f9d0>
    
    >>> class C(object):
    ...     def __str__(self):
    ...             return "A C Object"
    ... 
    >>> print C()
    A C Object
    >>>                                                                                         
    

    If you don't have control of the class... well, the author didn't implement a pretty view of the class. You could create subclasses with the methods or write a function to pull out the stuff you want.

    0 讨论(0)
提交回复
热议问题