Skip to content

__str__ method for ggplot objects #453

Closed

Description

@rhalbersma

I would be nice if ggplot objects had a __str__ method that draws the plot without the <ggplot:hash> annotation. For pretty printing, users could then write print(p) and for debugging print(repr(p)) would still be available.

Activity

  1. has2k1 commented on Oct 29, 2020

    @has2k1
    Owner

    What is your main working environment? Jupyter-notebook, pycharm, ... ? If not jupyter-notebook (it uses __repr__), does it use __str__ or __repr__ for printing the last statement of a code block/cell?

  2. rhalbersma commented on Oct 29, 2020

    @rhalbersma
    Author

    I am working in a Jupyter-notebook but from within Visual Studio Code. If Jupyter-notebook would call __repr__ then I can still do print(str(p)) and then print(p) would even be backwards compatible so that existing user code is unaffected.

  3. has2k1 commented on Oct 30, 2020

    @has2k1
    Owner

    So, this is how the cases would turn out

    1. repr(p) - Show Plot, Return '<ggplot:hash>'
    2. str(p) - Show Plot, Return ''
    3. print(p) - Show Plot, Print ''
    4. print(str(p)) - Show Plot, Print ''

    am I missing anything?

  4. rhalbersma commented on Oct 30, 2020

    @rhalbersma
    Author

    So, this is how the cases would turn out

    1. repr(p) - Show Plot, Return '<ggplot:hash>'
    2. str(p) - Show Plot, Return ''
    3. print(p) - Show Plot, Print ''
    4. print(str(p)) - Show Plot, Print ''

    am I missing anything?

    AFAIK, currently, print(p) does the same as repr(p), so the two primitives would be repr(p) and str(p) and users can forget about doing print(p) altogether, except to do more complicated printing.

  5. has2k1 commented on Oct 30, 2020

    @has2k1
    Owner

    AFAIK, currently, print(p) does the same as repr(p)

    Yes that is the case; and it is what is going to change.

  6. rhalbersma commented on Oct 30, 2020

    @rhalbersma
    Author

    Yes, so people who really like the hash codes in their publication-ready plots, need to change their code to repr(p). So it's not backward compatible.

  7. added a commit that references this issue on Nov 28, 2020
    af4d9b4
  8. added a commit that references this issue on Sep 5, 2024
    5e90856
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions