Good or bad use of __repr__?

A

Alaric Haag

Hello,

Is the use of __repr__ below a "really bad idea"?

class Dimension():
def __init__(self, setp, name):
ptr = setp.contents.dim
while ptr.contents.name != name:
ptr = ptr.contents.next
self.name = ptr.contents.name
self.size = ptr.contents.size
self.unlimited = bool(ptr.contents.unlimited)
self.coord = ptr.contents.coord
def __repr__(self):
return '%g' % (self.size)

As written, if a program references a Dimension instance without an
attribute, it gets the size attrbute "by default". If it wants the other
attributes, they have to be spec'd. In the context of the code being
developed, the "size" attribute is the "logical" representation of the
dimension. I'm just wondering if this sort of design should be avoided.

Many thanks!

Alaric
 
S

Steven D'Aprano

Hello,

Is the use of __repr__ below a "really bad idea"?

class Dimension():
def __init__(self, setp, name):
ptr = setp.contents.dim
while ptr.contents.name != name:
ptr = ptr.contents.next
self.name = ptr.contents.name
self.size = ptr.contents.size
self.unlimited = bool(ptr.contents.unlimited)
self.coord = ptr.contents.coord
def __repr__(self):
return '%g' % (self.size)


As a rule of thumb, you should aim for:

eval( repr(obj) )

to recreate the obj. That's not always possible, but when possible, it is
an ideal to aspire to. Given that, I'd recommend:

def __repr__(self):
return '%s(%s, %s)' % (
self.__class__.__name__, self.ptr, self.name)
def __str__(self):
return "<dim=%g>" % self.size


except of course your class doesn't store ptr.


But looking at the code shown, I'm guessing you have bigger design
problems than just what __repr__ should look like. I suggest you read
this:

http://www.surfscranton.com/architecture/LawOfDemeter.htm
 
S

Steven D'Aprano

... I'd recommend:

def __repr__(self):
return '%s(%s, %s)' % (
self.__class__.__name__, self.ptr, self.name)
def __str__(self):
return "<dim=%g>" % self.size


except of course your class doesn't store ptr.

Ah crap, sorry, I meant setp not ptr. Sorry for the confusion.
 
B

Bruno Desthuilliers

Alaric Haag a écrit :
Hello,

Is the use of __repr__ below a "really bad idea"?

I'd probably do the same as Stephen Hansen ("<Dimension(size=50)>") or
at least something quite similar.


Now on a totally unrelated point (micro optimization anyone ?):
class Dimension():
def __init__(self, setp, name):
ptr = setp.contents.dim
while ptr.contents.name != name:
ptr = ptr.contents.next
self.name = ptr.contents.name
self.size = ptr.contents.size
self.unlimited = bool(ptr.contents.unlimited)
self.coord = ptr.contents.coord

In the above code, you're constantly refering to ptr.contents, and never
use ptr directly. Attribute lookup is not free, so it's better to avoid
them. Local bindings lookup, OTHO, is quite fast. IOW, this would better
be written as:

def __init__(self, setp, name):
contents = setp.contents.dim
while contents.name != name:
contents = contents.next
self.name = contents.name
self.size = contents.size
self.unlimited = bool(contents.unlimited)
self.coord = contents.coord
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top