[Tutor] Good approach regarding classes attributes
Peter Otten
__peter__ at web.de
Mon Sep 8 18:04:41 CEST 2014
Juan Christian wrote:
> On Mon, Sep 8, 2014 at 5:58 AM, Peter Otten <__peter__ at web.de> wrote:
>> Personally I'd use normal attributes, though.
>>
>
> Why normal attributes? Isn't it better to make these read-only as I won't
> ever need to modify them? And even if I modify them, it won't change in
> the Steam servers, only in my program, and I don't see any use for that, I
> need the 'real' values always, the 'real' thing only.
Yes, but you know that those attributes should not be reassigned, and for
everyone else a hint in the docstring will do. Basically I tend to write as
little code as needed, or to put it another way: I'm a lazy bastard ;)
In that spirit here's an alternative implementation of the User class:
from collections import namedtuple
User = namedtuple(
"User",
"steamid personaname lastlogoff profileurl "
"avatar timecreated countrycode")
You may note that I refrained from making little improvements to the
attribute names -- another odd preference of mine ;) -- which also helps
simplify the fetch_users() implementation:
...
for user in user_list:
if user["communityvisibilitystate"] == 3:
users.append(User._make(user[field] for field in User._fields))
...
More information about the Tutor
mailing list