On 07/09/14 04:00, Juan Christian wrote:
I'm writing a program that have a 'User' class. This class will have the
following attributes:
1. id
2. personaname
3. lastlogoff
4. profileurl
5. avatar
6. realname
7. timecreated
8. loccountrycode
I'm thinking about writing something like that: http://pastebin.com/7KHB2qQ8
When its a short bit of code (<100 lines) just put it in
the email body...
class User():
def __init__(id):
self.__id = id
[URL Request to call API and get everything using the ID (JSON)]
self.__personaname = [JSON response personaname]
self.__lastlogoff = [JSON response personaname]
[...]
This is fine although the question of whether you need the attributes to
be private needs to be considered carefully on a per attribute basis.
def get_id():
return __id
def get_personaname():
return __personaname
But this style is not Pythonic. If you have a method that
just returns the attribute its better to just make the attribute
non-private and allow users to access it directly.
In fact even if you wanted to do some processing around
the access, rather than have lots of getXXX methods it
would be more Pythonic to write get/set methods but then
declare the attribute as a property and hide the get/set
methods so that, to the user, it looks like direct access.
get/set methods are a very Java-ish kind of style but
not really Pythonic. For most cases direct access is
preferred.
Also, as a matter of style/convention, the class name is
usually CamelCased so it would be
class User:
Similarly attribute names are usually spaced using
underscores so personaname becomes persona_name etc.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor