[Tutor] Good approach regarding classes attributes

Joel Goldstick joel.goldstick at gmail.com
Mon Sep 8 04:49:38 CEST 2014


On Sun, Sep 7, 2014 at 10:31 PM, Juan Christian
<juan0christian at gmail.com> wrote:
> So... I tried to follow all what you guys said:
>
> user.py module:
>
> import urllib.request
> import json
>
> class User():
>
> def __init__(self, steamid, personaname, lastlogoff, profileurl, avatar,
> timecreated, loccountrycode):
> self._steamid = steamid
> self._personaname = personaname
> self._lastlogoff = lastlogoff
> self._profileurl = profileurl
> self._avatar = avatar
> self._timecreated = timecreated
> self._loccountrycode = loccountrycode
>
>
> @property
> def steamid(self):
>    return self._steamid

I don't understand the purpose of the decorator and the _business
why not self.timecreated = timecreated   etc?
>
> @property
> def personaname(self):
> return self._personaname
>
Unless the formatting got screwed up in the email, all or these return
statements need to be indented
> @property
> def lastlogoff(self):
> return self._lastlogoff
>
> @property
> def profileurl(self):
> return self._profileurl
>
> @property
> def _avatar(self):
> return self._avatar
>
> @property
> def _timecreated(self):
> return self._timecreated
>
> @property
> def _loccountrycode(self):
> return self._loccountrycode
>
>
> def fetch_user(steamid):
> req =
> urllib.request.urlopen('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=B9F55D955257F1EDC9B6D217B94FCD20&steamids='
> + steamid)
> content = json.loads(req.read().decode('utf-8'))["response"]["players"][0]
> print("DEBUG ONLY: " + content["avatar"] + "\n")

try this:
print content
>
> return User(content["steamid"], content["personaname"],
> content["lastlogoff"], content["profileurl"], content["avatar"],
> content["timecreated"], content["loccountrycode"])
>
>
> main module:
>
> from user import User
>
> u = User.fetch_user("76561198067618735")
> print(u)
>
>
> console output:
>
> DEBUG ONLY:
> http://media.steampowered.com/steamcommunity/public/images/avatars/da/da259bfaef7fe7c2521de78433977a6c006217
> c5.jpg
>
> Traceback (most recent call last):
>   File ".\main.py", line 3, in <module>
>     u = User.fetch_user("76561198067618735")
>   File "D:\Documents\HomeBroker\user.py", line 50, in fetch_user
>     return User(content["steamid"], content["personaname"],
> content["lastlogoff"], content["profileurl"], content["avata
> r"], content["timecreated"], content["loccountrycode"])
>   File "D:\Documents\HomeBroker\user.py", line 11, in __init__
>     self._avatar = avatar
> AttributeError: can't set attribute
>
>
> Why am I getting this "AttributeError: can't set attribute" specifically
> when trying to set 'self._avatar = avatar'?

Why not print out all of the content data.  Something around avatar
might look strange.
>
> Does my code in user.py module follows the pythonic way now?
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Joel Goldstick
http://joelgoldstick.com


More information about the Tutor mailing list