repr

How can we get the default behavior of __repr__()?

孤者浪人 提交于 2021-01-21 12:13:36
问题 If someone writes a class in python, and fails to specify their own __repr__() method, then a default one is provided for them. However, suppose we want to write a function which has the same, or similar, behavior to the default __repr__() . However, we want this function to have the behavior of the default __repr__() method even if the actual __repr__() for the class was overloaded. That is, suppose we want to write a function which has the same behavior as a default __repr__() regardless of

Python __repr__ for all member variables

徘徊边缘 提交于 2020-02-27 02:25:10
问题 Implementing __repr__ for a class Foo with member variables x and y , is there a way to automatically populate the string? Example that does not work: class Foo(object): def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return "Foo({})".format(**self.__dict__) >>> foo = Foo(42, 66) >>> print(foo) IndexError: tuple index out of range And another: from pprint import pprint class Foo(object): def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return "Foo({})"

Python __repr__ for all member variables

会有一股神秘感。 提交于 2020-02-27 02:24:23
问题 Implementing __repr__ for a class Foo with member variables x and y , is there a way to automatically populate the string? Example that does not work: class Foo(object): def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return "Foo({})".format(**self.__dict__) >>> foo = Foo(42, 66) >>> print(foo) IndexError: tuple index out of range And another: from pprint import pprint class Foo(object): def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return "Foo({})"

Formatting floating-point numbers without loss of precision in AngularJS

孤街醉人 提交于 2020-01-02 07:25:16
问题 In AngularJS how do I output a floating point number on an HTML page without loss of precision and without unnecessary padding with 0's? I've considered the "number" ng-filter (https://docs.angularjs.org/api/ng/filter/number) but the fractionSize parameter causes a fixed number of decimals: {{ number_expression | number : fractionSize}} I'm looking for what in various other languages is referred to as "exact reproducibility", "canonical string representation", repr, round-trip, etc. but I

Python: Maximum recursion depth exceeded when printing custom exception

亡梦爱人 提交于 2020-01-02 01:46:29
问题 The following code throws RuntimeError: maximum recursion depth exceeded while getting the str of an object . I can resolve the infinite recursion in two different ways, but I don't understand why each fix works and thus don't know which to use, or if either are correct. class FileError( Exception ): def __init__( self, filename=None, *a, **k ): #Fix 1: remove super super( FileError, self ).__init__( self, *a, **k ) self.filename = filename def __repr__( self ): return "<{0} ({1})>".format(

When is the output of repr useful?

会有一股神秘感。 提交于 2019-12-21 16:59:00
问题 I have been reading about repr in Python. I was wondering what the application of the output of repr is. e.g. class A: pass repr(A) ='<class __main__.A at 0x6f570>' b=A() repr(b) = '<__main__.A instance at 0x74d78>' When would one be interested in '<class __main__.A at 0x6f570>' or '<__main__.A instance at 0x74d78>' ? 回答1: Sometimes you have to deal with or present a byte string such as bob2='bob\xf0\xa4\xad\xa2' If you print this out (in Ubuntu) you get In [62]: print(bob2) bob𤭢 which is not

Tell IPython to use an object's `__str__` instead of `__repr__` for output

你说的曾经没有我的故事 提交于 2019-12-19 17:32:13
问题 By default, when IPython displays an object, it seems to use __repr__ . __repr__ is supposed to produce a unique string which could be used to reconstruct an object, given the right environment. This is distinct from __str__ , which supposed to produce human-readable output. Now suppose we've written a particular class and we'd like IPython to produce human readable output by default (i.e. without explicitly calling print or __str__ ). We don't want to fudge it by making our class's __repr__

Force repr() to use single quotes

五迷三道 提交于 2019-12-18 06:48:46
问题 I have a question, is there a way to "force" repr() to create always single quotes around a string? This happens when I only use repr() print repr("test") 'test' print repr("test'") "test'" print repr("test\"") 'test"' print repr("test'\"") 'test\'"' so the last one actually does, what I want, but I don't want to add always \\" to get the single quotes. Edit: I am not going to mark an answer as accepted since, as pointed out by @martijn-pieters, I was using repr() for purposes it is not

Best output type and encoding practices for __repr__() functions?

喜你入骨 提交于 2019-12-17 23:22:21
问题 Lately, I've had lots of trouble with __repr__() , format() , and encodings. Should the output of __repr__() be encoded or be a unicode string? Is there a best encoding for the result of __repr__() in Python? What I want to output does have non-ASCII characters. I use Python 2.x, and want to write code that can easily be adapted to Python 3. The program thus uses # -*- coding: utf-8 -*- from __future__ import unicode_literals, print_function # The 'Hello' literal represents a Unicode object

In Python, what does '<function at …>' mean?

眉间皱痕 提交于 2019-12-17 16:58:21
问题 What does <function at 'somewhere'> mean? Example: >>> def main(): ... pass ... >>> main <function main at 0x7f95cf42f320> And maybe there is a way to somehow access it using 0x7f95cf42f320 ? 回答1: You are looking at the default representation of a function object. It provides you with a name and a unique id, which in CPython happens to be a memory address. You cannot access it using the address; the memory address is only used to help you distinguish between function objects. In other words,