Manim: VGroup and VDict must override __repr__ to display the mobjects contained

Created on 30 Sep 2020  路  7Comments  路  Source: ManimCommunity/manim

Consider this example:

class Test(Scene):
    def construct(self):
        sq = Square()
        cir = Circle()
        grp = VGroup(sq, cir)
        print(grp) # --> Just prints: VGroup
        self.play(ShowCreation(grp))
        dct = VDict({'s' : sq, 'c' : cir}).next_to(grp, DOWN)
        print(dct) # --> Just prints: VDict
        self.play(ShowCreation(dct))

When there are _a lot_ of mobjects in the VGroup or VDict, it sometimes helps to know what are those mobjects while debugging.
Something like
[Square, Circle] or even better the full hash name (IDK what it's called) -- manim.mobject....at 0x00....
Let me know how can this be best incorporated.

enhancement good first issue

All 7 comments

You have to define the __str__ method of VGriup/VDict. I think some Mobjects already have a __str__ method so it should be enough to do something like return ", ".join(str(m) for m in self.mobjects)

Seems to be an easy PR, if no one object

Would like to know what would be preferred just Square or the full hash name? I can make a quick PR today if no one minds.

I would prefer implementing __repr__ over __str__ (see https://stackoverflow.com/questions/1436703/difference-between-str-and-repr), and would love to see more manim classes having proper __repr__ methods. (Note in particular: if __repr__ is defined and __str__ is not, str(object) will still work.)

For the container, I fully agree that its repr should print a list of its children. But for something concrete like Square I would find it useful to let Square(side_length=5) print something like 'Square with side length 5', or even 'Square with side length 5 and center (0., 0., 0.)'; the more explicit and defining the better. (For your PR it is fine to just take care of VGroup and VDict, improving their representation strings is definitely more important. 馃憤)

Would like to know what would be preferred just Square or the full hash name? I can make a quick PR today if no one minds.

What hash ?

What hash ?

This
<manim.mobject.geometry.Square object at 0x000001AA9A5E7790>
I am not sure what's the proper name for this - I call this "printing the object"

I don't know the proper name either lol, but the hex thing at the end is the id, I think.
And I think the id in the repr _can_ be sometimes useful, when one wants to check if two objects are different.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

huguesdevimeux picture huguesdevimeux  路  5Comments

PgBiel picture PgBiel  路  5Comments

dakyion picture dakyion  路  8Comments

cobordism picture cobordism  路  5Comments

naveen521kk picture naveen521kk  路  4Comments