[Tutor] setattr vs __setattr__
Rasjid Wilcox
rasjidw at gmail.com
Tue Sep 7 06:18:34 CEST 2010
On 7 September 2010 08:55, Steven D'Aprano <steve at pearwood.info> wrote:
> On Mon, 6 Sep 2010 09:03:30 pm Rasjid Wilcox wrote:
>> I've been using
>>
>> for attr_name in name_list:
>> setattr(a, attr_name, getattr(b, attr_name))
>>
>> to copy the attributes from one type of class to another, and it is
>> not quite as readable as I would like.
>
> The one-liner in the for loop is very concise. The problem is that
> concise is often the opposite of readable. So make it less concise:
>
> for name in name_list:
> obj = getattr(b, name)
> setattr(a, name, obj)
>
> Does that help?
Yes, that does help. Such a simple and obvious thing (once someone
has pointed it out) and addresses my concern with the one-liner that
while easy to write, when looking back at it one has to look carefully
and analyse it to be certain about what is going on. The two liner
has a few more keystrokes but is much easier on the eyes. And easy on
the eyes is important to me - it is why I find Python 'beautiful' in a
way that most programming languages are not.
Much thanks,
Rasjid.
More information about the Tutor
mailing list