Skip to content

Text class: changed __str__ method and added __repr__ method #6068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 28, 2016
Merged

Text class: changed __str__ method and added __repr__ method #6068

merged 2 commits into from
Feb 28, 2016

Conversation

jonchar
Copy link
Contributor

@jonchar jonchar commented Feb 27, 2016

This should address #6042

According to the Python docs for __repr__ and __str__: print() will output what is returned by __str__, while __repr__ should return a string that looks like a valid python expression. The text class currently has a __str__ but no __repr__. This changes the existing __str__ method into __repr__ (which returns Text(x_coord, y_coord, "string")) and adds a new __str__ method that returns self._text, which achieves the behavior requested in #6042 .

This would result in the following behavior:

>>> text
Text(x_coord,y_coord,"String in self._text")
>>> print(text)
String in self._text

Thoughts?

@tacaswell
Copy link
Member

👍 on the change __repr__, 50/50 on __str__ just returning the text. What is the use case for wanting to print a Text instance and have the fact that it is a Text instance hidden?

@jonchar
Copy link
Contributor Author

jonchar commented Feb 27, 2016

I was going on what was requested in #6042, however I can't think of when it would be useful for print to hide the fact that it's a Text instance and not a string. Maybe just keep __repr__ and leave out __str__? If there's no __str__ defined __repr__ will be used instead.

@tacaswell
Copy link
Member

I like the option of just dropping __str__.

@jonchar
Copy link
Contributor Author

jonchar commented Feb 28, 2016

__str__ method has been dropped in favor of __repr__

tacaswell added a commit that referenced this pull request Feb 28, 2016
MNT: remane __str__ to __repr__ for Text
@tacaswell tacaswell merged commit 90a6984 into matplotlib:master Feb 28, 2016
@tacaswell
Copy link
Member

Thanks!

On Sun, Feb 28, 2016, 10:35 Jon Charest notifications@github.com wrote:

str method has been dropped in favor of repr


Reply to this email directly or view it on GitHub
#6068 (comment)
.

@QuLogic QuLogic added this to the 2.1 (next point release) milestone Feb 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants