Re: Dealing with the __str__ method in classes with lots of attributes

2012-05-14 Thread Karl Knechtel
On Sat, May 12, 2012 at 9:10 AM, Ethan Furman wrote: > > Firstly, __slots__ is a tuple. I object: conceptually, the "slots" of a class are set in stone, but the `__slots__` attribute of a class object is just an attribute, and any iterable (as long as it yields valid identifier names) can be used

RE: Dealing with the __str__ method in classes with lots of attributes

2012-05-14 Thread Andreas Tawn
> p.s. Is Python seeing a lot of use at Ubisoft or is this just for personal > interest (or > perhaps both)? We do use Python a fair bit, mostly for build systems and data mining, but also because it's the built-in script language for Motionbuilder. -- http://mail.python.org/mailman/listinfo/py

Re: Dealing with the __str__ method in classes with lots of attributes

2012-05-12 Thread Ethan Furman
Karl Knechtel wrote: On Thu, May 10, 2012 at 9:33 AM, Andreas Tawn wrote: And there's also something like... return "\n".join((": ".join((str(k), str(self.__dict__[k]))) for k in self.__dict__)) which is a nice length, but I lose control of the order of the attributes and the formatting is

Re: Dealing with the __str__ method in classes with lots of attributes

2012-05-12 Thread Karl Knechtel
On Thu, May 10, 2012 at 9:33 AM, Andreas Tawn wrote: > And there's also something like... > > return "\n".join((": ".join((str(k), str(self.__dict__[k]))) for k in > self.__dict__)) > > which is a nice length, but I lose control of the order of the attributes and > the formatting is fixed. It al

Re: Dealing with the __str__ method in classes with lots of attributes

2012-05-11 Thread Cameron Simpson
On 11May2012 15:40, Mark Lawrence wrote: | On 11/05/2012 15:32, Andreas Tawn wrote: | > It's also helpful to not have to display every attribute, of which there may be dozens. | | Do I detect a code smell here? Not necessarily. (Well, yeah, "dozens" may indicate time to partition stuff.) My "O"

RE: Dealing with the __str__ method in classes with lots of attributes

2012-05-11 Thread Andreas Tawn
> >> It's also helpful to not have to display every attribute, of which > >> there may be dozens. > > > > Do I detect a code smell here? > > > I think so, Murphy's law dictates that the attribute you're interested in > will not be > displayed anyway. That's what __repr__'s for. -- http://mail.py

RE: Dealing with the __str__ method in classes with lots of attributes

2012-05-11 Thread Andreas Tawn
> > It's also helpful to not have to display every attribute, of which there > > may be > dozens. > > Do I detect a code smell here? Possibly. I'll often try to subdivide into several simpler types, but sometimes that makes the code more complex than it needs to be. -- http://mail.python.org/m

Re: Dealing with the __str__ method in classes with lots of attributes

2012-05-11 Thread Jean-Michel Pichavant
Mark Lawrence wrote: On 11/05/2012 15:32, Andreas Tawn wrote: It's also helpful to not have to display every attribute, of which there may be dozens. Do I detect a code smell here? I think so, Murphy's law dictates that the attribute you're interested in will not be displayed anyway. JM -

Re: Dealing with the __str__ method in classes with lots of attributes

2012-05-11 Thread Mark Lawrence
On 11/05/2012 15:32, Andreas Tawn wrote: It's also helpful to not have to display every attribute, of which there may be dozens. Do I detect a code smell here? -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list

RE: Dealing with the __str__ method in classes with lots of attributes

2012-05-11 Thread Andreas Tawn
> I have no idea why using __repr__ versus __str__ would make any difference in > the > order of the attributes. They're going to come out in the order you specify, > regardless of what you name your method. If you don't like the arbitrary > order you > get from the dictionary, then either sort

Re: Dealing with the __str__ method in classes with lots of attributes

2012-05-11 Thread Dave Angel
On 05/11/2012 07:16 AM, Andreas Tawn wrote: >> >> This is a very interesting solution. >> >> I think it might be better suited (for my purpose) to __repr__ rather than >> __str__, mostly because I still lose control of the order the attributes >> appear. I have no idea why using __repr__ versus

RE: Dealing with the __str__ method in classes with lots of attributes

2012-05-11 Thread Andreas Tawn
> This issue bit me once too often a few months ago, and now I have a class > called > "O" from which I often subclass instead of from "object". > Its main purpose is a friendly __str__ method, though it also has a friendly > __init__. > > Code: > > class O(object): > ''' A bare objec

Re: Dealing with the __str__ method in classes with lots of attributes

2012-05-10 Thread Cameron Simpson
On 10May2012 15:33, Andreas Tawn wrote: | Say I've got a class... | | class test(object): | def __init__(self): | self.foo = 1 | self.bar = 2 | self.baz = 3 | | I can say... | | def __str__(self): |return "foo: {0}\nbar: {1}\nbaz: {2}".format(self.foo, self.bar,

Re: Dealing with the __str__ method in classes with lots of attributes

2012-05-10 Thread Chris Angelico
On Fri, May 11, 2012 at 1:15 AM, Andreas Tawn wrote: > I considered the triple quote string one, but it's not very PEP 8 compatible > in a real class because it includes the indentation in the formatted string. Yeah, that is an annoying side effect. My personal view is that code should be writte

RE: Dealing with the __str__ method in classes with lots of attributes

2012-05-10 Thread Andreas Tawn
> On Thu, May 10, 2012 at 11:33 PM, Andreas Tawn > wrote: > > Say I've got a class... > > > > class test(object): > >    def __init__(self): > >        self.foo = 1 > >        self.bar = 2 > >        self.baz = 3 > > > > I can say... > > > > def __str__(self): > >   return "foo: {0}\nbar: {1}\nbaz

Re: Dealing with the __str__ method in classes with lots of attributes

2012-05-10 Thread Chris Angelico
On Thu, May 10, 2012 at 11:33 PM, Andreas Tawn wrote: > Say I've got a class... > > class test(object): >    def __init__(self): >        self.foo = 1 >        self.bar = 2 >        self.baz = 3 > > I can say... > > def __str__(self): >   return "foo: {0}\nbar: {1}\nbaz: {2}".format(self.foo, self

Dealing with the __str__ method in classes with lots of attributes

2012-05-10 Thread Andreas Tawn
Say I've got a class... class test(object): def __init__(self): self.foo = 1 self.bar = 2 self.baz = 3 I can say... def __str__(self): return "foo: {0}\nbar: {1}\nbaz: {2}".format(self.foo, self.bar, self.baz) and everything's simple and clean and I can vary the f