| Start |
|---|
| Motivation So you've just created your first class in Python, a neat little class that encapsulates a playing card: Elsewhere in your code, you create a few instances of this class: You've even created a list of cards, in order to represent a "hand": Now, during debugging, you want to see what your hand looks like, so you do what comes naturally and write: But what you get back is a bunch of gibberish: Confused, you try just printing a single card: And again, you get this weird output: Have no fear. We're about to fix this. First, however, it's important to understand what's going on here. When you wrote That output is comprised of two important bits: the What really went on was that you asked Python to "put into words" the essence of that object and then display it to you. A more explicit version of the same machinery might be: In the first line, you try to turn your The ProblemThe issue you're encountering arises due to the fact that, while you told Python everything it needed to know about the And since it didn't know, when you (implicitly) wrote The Solution (Part 1)But we can tell Python how we want instances of our custom classes to be converted to strings. And the way we do this is with the Whenever you tell Python to create a string from a class instance, it will look for a Consider the following, updated version of our Here, we've now defined the (Note that "returns" is in bold here, to stress the importance of returning a string, and not simply printing it. Printing it may seem to work, but then you'd have the card printed when you did something like The Returning to our problem of displaying the card in a more user-friendly manner, if we again run: We'll see that our output is much better: And, to our surprise, we get those funny hex codes again: What's going on? We told Python how we wanted our The Solution (Part 2)Well, the behind-the-scenes machinery is a bit different when Python wants to get the string representation of items in a list. It turns out, Python doesn't care about Instead, it looks for a different method, So you're saying I have to make two methods to do the same thing? One for when I want to No, but first let's look at what our class would be like if we were to implement both Here, the implementation of the two methods Note that just like our We can see now what method is responsible for each case: As was covered, the At this point it's worth pointing out that just as we can explicitly create a string from a custom class instance using For example: And additionally, if defined, we could call the methods directly (although it seems a bit unclear and unnecessary): About those duplicated functions...Python developers realized, in the case you wanted identical strings to be returned from So instead, there is a mechanism in place to eliminate the need for that. One I snuck you past up to this point. It turns out that if a class implements the So, to be clear, consider the following version of the Note this version only implements the as do calls to SummaryIn order for you to empower your class instances to "show themselves" in user-friendly ways, you'll want to consider implementing at least your class's If you want different representations for when, for example, inside a container, you'll want to implement both |
Both methods implemented, eval-round-trip style repr() RemarksA note about implemeting both methodsWhen both methods are implemented, it's somewhat common to have a In fact, the Python docs for For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. What that means is that This string could be passed directly back into An example of an implementation of such a method might be: Notes[1] This output is implementation specific. The string displayed is from cpython. [2] You may have already seen the result of this |
Read Article
https://develop.horje.com/learn/1434/reference