About 707,000 results
Open links in new tab
  1. python - How can I choose a custom string representation for a class ...

    I would like to create some kind of class decorator, so I can easily set custom string representations for my classes without having to write a metaclass for each of them.

  2. How do I change the string representation of a Python class?

    Nov 6, 2015 · In Java, I can override the toString() method of my class. Then Java's print function prints the string representation of the object defined by its toString(). Is there a Python equivalent to Java's

  3. Does Python have a toString () equivalent, and can I convert a class to ...

    153 In python, the str() method is similar to the toString() method in other languages. It is called passing the object to convert to a string as a parameter. Internally it calls the __str__() method of the …

  4. String-based enum in Python - Stack Overflow

    Oct 29, 2019 · Formatted string literals, str.format (), and format () will use the mixed-in type’s format () unless str () or format () is overridden in the subclass, in which case the overridden methods or …

  5. python - Getting value of enum on string conversion - Stack Overflow

    The single-underscore attributes are internal to the generated enum class; better stick to the documented attribute (which happens to be a special descriptor so that you can still use value as a …

  6. Convert string to Python class object? - Stack Overflow

    Jul 24, 2009 · Given a string as user input to a Python function, I'd like to get a class object out of it if there's a class with that name in the currently defined namespace. Essentially, I want the implementat...

  7. How to change any data type into a string? - Stack Overflow

    Jul 8, 2010 · 75 str is meant to produce a string representation of the object's data. If you're writing your own class and you want str to work for you, add:

  8. python - What is the difference between <class 'str'> and <type 'str ...

    Feb 5, 2017 · for ID in response: response is a string and enumerating it gives the characters in the string. Depending on the type of response you may want to use and HTML, JSON or other parser to …

  9. python - How to print instances of a class using print ... - Stack Overflow

    As Chris Lutz explains, this is defined by the __repr__ method in your class. From the documentation of repr(): For many types, this function makes an attempt to return a string that would yield an object …

  10. Convert string to Enum in Python - Stack Overflow

    Dec 31, 2016 · In Python 3.11 you could also use a StrEnum. Granted, you would have to replace the values with strings as well, since it keyes on the value, not name (using enum.auto() for the value is …