Good or bad use of __repr__?
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Wed Feb 4 03:51:25 EST 2009
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
More information about the Python-list
mailing list