checking if an object IS in a list

John Machin sjmachin at lexicon.net
Sat Jul 19 17:55:23 EDT 2008


On Jul 20, 6:13 am, nicolas.pource... at gmail.com wrote:
> On 18 juil, 17:52, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
>
> > On Fri, 18 Jul 2008 07:39:38 -0700, nicolas.pourcelot wrote:
> > > So, I use something like this in 'sheet.objects.__setattr__(self,
> > > name, value)':
> > > if type(value) == Polygon:
> > >     for edge in value.edges:
> > >         if edge is_in sheet.objects.__dict__.itervalues():
> > >             object.__setattr__(self, self.__new_name(), edge)
>
> > > Ok, I suppose it's confused, but it's difficult to sum up. ;-)
>
> > You are setting attributes with computed names?  How do you access them?
> > Always with `gettattr()` or via the `__dict__`?  If the answer is yes, why
> > don't you put the objects the into a dictionary instead of the extra
> > redirection of an objects `__dict__`?
>
> Yes, I may subclass dict, and change its __getitem__ and __setitem__
> methods, instead of changing objets __setattr__ and __getattr__... But
> I prefer
>
> >>> sheet.objects.A = Point(0, 0)
> than
> >>> sheet.objects["A"] = Point(0, 0)
> > Oh and the `type()` test smells like you are implementing polymorphism
> > in a way that should be replaced by OOP techniques.
>
> I wrote 'type' here by mistake, but I used 'isinstance' in my
> code. ;-)
>
> > If you make Point immutable you might be able to drop the "must not be
>
> referenced twice" requirement.
>
> Yes, but unfortunately I can't (or it would require complete
> redesign...)

(1) You are searching through lists to find float objects by identity,
not by value
(2) Peter says he doesn't understand
(3) Marc thinks it smells

IOW, the indications are that it *already* requires complete redesign.



More information about the Python-list mailing list