[Tutor] Good approach regarding classes attributes

Juan Christian juan0christian at gmail.com
Mon Sep 8 04:31:52 CEST 2014


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

@property
def personaname(self):
return self._personaname

@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")

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'?

Does my code in user.py module follows the pythonic way now?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140907/a83c8313/attachment.html>


More information about the Tutor mailing list