[Biopython-dev] GSoC code+documentation review: PhyloXML for Biopython

Peter biopython at maubp.freeserve.co.uk
Sat Jul 4 12:39:13 EDT 2009


On Sat, Jul 4, 2009 at 5:28 PM, Eric Talevich<eric.talevich at gmail.com> wrote:
> On Sat, Jul 4, 2009 at 10:14 AM, Peter <biopython at maubp.freeserve.co.uk>wrote:
>
>>
>> The __repr__ thing isn't Biopython specific, its just what Python does. For
>> simple objects, eval(repr(obj)) should recreate the object. Consider:
>>
>> >>> print phx.other
>> [Other(tag=alignment, namespace=http://example.org/align)]
>>
>> That is odd to me. It looks like "other" is a list, containing an "Other"
>> object, but with a funny __repr__ - I would have expected it to look more
>> like this:
>>
>> >>> print phx.other
>> [Other(tag="alignment", namespace="http://example.org/align")]
>>
>> i.e. using the repr of what I have assumed are string arguments.
>>
>> Peter
>>
>
> Hi Peter,
>
> Thanks! Your interpretation of the example is correct. I'll change __repr__
> to check if the attribute is a string and, if so, escape and quote it.
>
> In the docs, I wrote that the representation is Biopython-style because by
> default, Python does something a little different for complex objects:
>
>>>> class Foo(object): pass
>>>> Foo()
> <__main__.Foo object at 0xb7cff22c>

Yes, that is the Python default for a user defined object.

> But I noticed that Seq and other Biopython objects give a nicer
> representation that actually works as a constructor, so I tried
> to match that.

I'd have to think of some more examples, but other Python
modules try to have eval(repr(obj)) work for their (simpler)
objects.

If you can do it without risking a really long string, this is a good
idea. You'll notice the Seq object repr actually uses a truncated
sequence for long sequences - you won't want to accidentally
get the whole thing printed at the python prompt! Likewise
doing repr() on a SeqRecord doesn't give you the full object.

Peter


More information about the Biopython-dev mailing list