[Tutor] Good approach regarding classes attributes

Juan Christian juan0christian at gmail.com
Mon Sep 8 16:17:33 CEST 2014


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.


 On Mon, Sep 8, 2014 at 2:36 AM, Alan Gauld <alan.gauld at btinternet.com>
 wrote:

>
> Also note the underscore. It improves readability to break multi-words
> like that.
> For example several others have quoted this as 'person name' rather than
> 'persona name' - quite a different concept but hard to spot when all one
> word...


I tried to follow the variables from the Steam API, there they use
'personaname', 'lastlogoff', 'profileurl', 'loccountrycode' and so on,
everything without ' _ ', but now I'm doing the way you said, thanks!


   File "D:\Documents\HomeBroker\user.py", line 11, in __init__
>>      self._avatar = avatar
>> AttributeError: can't set attribute
>>
>
> Thats because you set the property name to the field name.
> Notice the difference:
>
> @property
> def profileurl(self):
> return self._profileurl
>
> @property
> def _avatar(self):
> return self._avatar
>
>
> The first sets the property to the non-underscore version
> The second uses underscore for both property and field name,
> thus making the field read-only. Be careful what you wish for...
>
> Another reason to only use properties when you *need* to
> make them read only (or to do some processing on reads
> or writes)


Yes, my fault there, I didn't see the ' _ ' in the beginning of the
function, but I do need them to be read-only, the user will never need to
modify them, and if he tries to modify let's say the name, ' persona_name =
"New Name Here" ', it would only change inside the program, the Steam
Servers would still use the old name, the 'real' name.


The new and now working code is the following:

import urllib.request
import json

class User():

def __init__(self, steamid, personaname, lastlogoff, profileurl, avatar,
timecreated, loccountrycode):
self._steam_id = steamid
self._persona_name = personaname
self._last_logoff = lastlogoff
self._profile_url = profileurl
self._avatar = avatar
self._time_created = timecreated
self._country_code = loccountrycode


@property
def steam_id(self):
    return self._steam_id

@property
def persona_name(self):
return self._persona_name

@property
def last_logoff(self):
return self._last_logoff

@property
def profile_url(self):
return self._profile_url

@property
def avatar(self):
return self._avatar

@property
def time_created(self):
return self._time_created

@property
def country_code(self):
return self._country_code


def fetch_users(*id_collection):
steamid = []
users = []

for user_id in id_collection:
steamid.append(user_id)

req = urllib.request.urlopen('
http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=B9FXXXXXD20&steamids='
+ ','.join(steamid))
user_list = json.loads(req.read().decode('utf-8'))["response"]["players"]

for user in user_list:
if user["communityvisibilitystate"] == 3: # Only people with open profile
and inventory are wanted
users.append(User(user["steamid"], user["personaname"], user["lastlogoff"],
user["profileurl"], user["avatar"], user["timecreated"],
user["loccountrycode"]))

return users


Remember that I do need these attributes to be read-only, so I need to use
the '@property' decoration. I modified the 'fetch_users' function in order
to accept any number of IDs, this way I can fetch various profiles in one
call. Any problems, suggestions now?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140908/501bb94d/attachment-0001.html>


More information about the Tutor mailing list