Questions tagged [repr]

Python built-in function that returns a string from an arbitrary object. Abbreviation for "representation".

docs: https://docs.python.org/3/library/functions.html#repr

281 questions
0
votes
1 answer

Should I use spaces or None in list of characters which should be joined in python?

I'm writing a program in python where I use a list which represents a board of tic tac toe. Since I was asking myself if I should use "None" or single spaces for free spaces, I've written these two pieces of code: #code 1: list1 = [None,…
Sven
  • 119
  • 1
  • 8
0
votes
1 answer

python %s string substitution and "self"?

see this code in pyyaml doc, why "Dice(%s,%s)" % self works ? there are two %s but only one var self? >>> class Dice(tuple): ... def __new__(cls, a, b): ... return tuple.__new__(cls, [a, b]) ... def __repr__(self): ... return…
Shuman
  • 3,914
  • 8
  • 42
  • 65
0
votes
1 answer

Python printing results in columns

I have been printing some of the output of my program the following way: a=[1,2,3] b=[10,20,30] c=[101,201,301] d=[1010,2010,3010] results = {'a':a,'b':b,'c':c,'d':d} print str("First result: ").center(20), str("Second result: ").center(20),…
Michael B
  • 94
  • 8
0
votes
1 answer

What pandas DataFrame method tells ipython notebook to display as HTML

I have created a class in which the main deliverable piece of data is stored in an attribute as a pandas DataFrame. I'd like the default display behavior of instances of this class to be the same as that of this DataFrame. Particularly when in…
piRSquared
  • 285,575
  • 57
  • 475
  • 624
0
votes
1 answer

Python Using PyAudio how to write microphone output to text file and read at receiver side to create wave file

i have code written as given below . It works perfectly . Now i want to save the output of microphone in text file . p = pyaudio.PyAudio() stream = p.open(format=pyaudio.paInt16, channels=18, rate=44100, …
0
votes
1 answer

Why can you use eval() with the function repr(y) inside and not str(y)?

Here is some code that I've found and would like for someone to explain. If I assign a string to the variable y, evaluate it with the eval function and assign the content to the variable y2, the interpreter returns true. >>> y = 'a string' >>> y2 =…
Luis Averhoff
  • 385
  • 6
  • 22
0
votes
1 answer

How to use __repr__ method python 2.7

I need to define a class of polygons in 2D. Each polygon defined by a list of tuples(coordinates) for example: poly = [(0.5,0),(1,0.5),(0.5,1),(0,0.5)] (There is no need for sorting) The points are sorted clockwise starting from the most left…
0
votes
1 answer

Representational form in string formating python

I've been learning Python rapidly and am getting confused with the representational form and string form of an object as well as the repr method. I call x = Point(1, 3) with the following code and get: class Point(): def __init__(self, x, y): …
q.Then
  • 2,743
  • 1
  • 21
  • 31
0
votes
3 answers

Getting variable name in of some duck-type classes

I have a Language class as such: class _Language: def __init__(self, name, bRightToLeft=False): self.name = name self.bRightToLeft = bRightToLeft def isRightToLeft(self): return self.bRightToLeft def…
alvas
  • 115,346
  • 109
  • 446
  • 738
0
votes
2 answers

Using __repr__ with shelve module in Python

I'm writing a wrapper class for the shelve module, and I'm intend to use it like a dictionary. Here's the code: import shelve class MyShelve: def __init__(self, filename='myshelve.db'): self.s = shelve.open(filename) def…
czayas
  • 495
  • 4
  • 6
0
votes
1 answer

Can someone explain how to use the repr function to format the output?

I would like the following code: Tier0 = ['Tier', 'Weights', 'Price', 'Number of Businesses', 'Revenue'] Tier1 = ['Tier 1', 180,] Tier2 = ['Tier 2', 300,] Tier3 = ['Tier 3', 450,] Tier4 = ['Tier 4', 600,] Tier5 = ['Tier 5', 750,] data =…
0
votes
3 answers

Access class variables with __repr__()

I'm relatively new to Python, and I was wondering how you can "configure" a class the way that it returns a tuple if you print it. Example: I have a class with the attributes a, b & c: class Foo: def __init__(self): self.a = 3 …
Nearoo
  • 4,454
  • 3
  • 28
  • 39
0
votes
1 answer

Cannot return string from python class

I'm trying to learn how to correctly use classes in python, I'm fairly new to it but I cant get this class to return a string output of all the values. Ideally I'd like to be able to just str(packet) into a network socket. class ARP(): dst_addr…
tozhan
  • 413
  • 1
  • 7
  • 18
0
votes
0 answers

Python - Description of repr()

I've read the documentation on repr() but I have a feeling that it may be more useful than the docs are giving it credit for. If my speculative feeling is correct, is someone able to provide instances with which repr() comes into its own.
Phoenix
  • 4,386
  • 10
  • 40
  • 55
0
votes
1 answer

Stuck with __repr__ function, not working as I understand it

I have a code which creates a graph, with nodes, and which keeps track of edges. This code itself appears to be working fine, but I cannot get the repr override to work how I would expect it to, after reading a few of the str vs repo and repr()…