checking if an object IS in a list
nicolas.pourcelot at gmail.com
nicolas.pourcelot at gmail.com
Sat Jul 19 16:13:40 EDT 2008
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...)
More information about the Python-list
mailing list