On Sun, Sep 7, 2014 at 5:04 AM, Peter Otten <__pete...@web.de> wrote: > > It's not a good approach and it's not pythonic. > > In Python you should avoid accessor functions and (pseudo-)private > __attributes ("Python is not Java"). So > > class User: > def __init__(self, id): > self.id = id > # load attributes > self.personname = [personname from JSON] > ... > > user = User(42) > > is certainly better. You might also consider making the class less > dependent > of the way you acquire the corresponding data: > > class User: # in Python 2: class User(object): ... > def __init__(self, id, personname, ...) > self.id = id > self.personname = personname > ... > @classmethod > def from_id(class_, id): > # load attributes > return User(id, personname, ...) > > jeff = User(42, "Jeff", ...) > jack = User.from_id(43)
Ok, no pseudo-private attributes. I read it in tutorials from 2013 and in the course that I'm taking that this would be a good pythonic way to deal with class attributes. They wouldn't be truly private, but someone using the program would see the " __ " in the beginning of the attribute and wouldn't call it directly, "because we are all adults and such", you saying that this approach doesn't exist in real life? I can't give all the 8 attributes to '__init__' because I don't even have them. I would call it with ID only and them the API server would return me all the info, and then I would set them. I didn't learn '@classmethod' decoration yet, but I presume it would work as a 'get()', right? The thing is, where 'user with id 43' is stored? You get it using 'from_id' but we don't have any list in there the store users, I got confused in that part.
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor