checking if an object IS in a list

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sun Jul 20 07:17:14 CEST 2008


On Sat, 19 Jul 2008 13:13:40 -0700, nicolas.pourcelot 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)

But with computed names isn't the difference more like

setattr(sheet.objects, name, Point(0, 0))
vs.
sheet.objects[name] = Point(0, 0)

and

getattr(sheet.objects, name)
vs.
sheet.objects[name]

Or do you really have ``sheet.objects.A`` in your code, in the hope that
an attribute named 'A' exists?

>> 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. ;-)

Doesn't change the "code smell".  OOP approach would be a method on the
geometric objects that know what to do instead of a type test to decide
what to do with each type of geometric object.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list