Re: [Tutor] Good approach regarding classes attributes

2014-09-11 Thread Danny Yoo
> I've just again experienced a new employer that tells my students my > name is 'Van Den Broek' when I tell them that it is 'van den Broek.' > This is the third time this week I've encountered this as a > programming example. Perhaps the use of the example is responsible for > the false belief amo

Re: [Tutor] Good approach regarding classes attributes

2014-09-11 Thread Brian van den Broek
On 7 September 2014 21:01, Danny Yoo wrote: > Let's use a concrete example: say that we'd like to make sure a > Person's name is always capitalized. We might try to enforce this > capitalization property in the constructor. Hi all, I've just again experienced a new employer that tells my

Re: [Tutor] Good approach regarding classes attributes

2014-09-09 Thread Alan Gauld
On 09/09/14 14:44, Peter Otten wrote: Is it not helpful to always put (object) as the parent, if the class is not itself a sub-class? The answer differs between Python 2 and 3. In Python 3 class C: # preferred in Python 3 pass Apologies, I should have mentioned that. I've been using P

Re: [Tutor] Good approach regarding classes attributes

2014-09-09 Thread Sydney Shall
On 09/09/2014 16:05, Joel Goldstick wrote: On Tue, Sep 9, 2014 at 10:02 AM, Sydney Shall wrote: On 09/09/2014 15:44, Peter Otten wrote: Sydney Shall wrote: On 08/09/2014 18:39, Alan Gauld wrote: On 08/09/14 15:17, Juan Christian wrote: One tiny tweak... class User(): You don't need the pa

Re: [Tutor] Good approach regarding classes attributes

2014-09-09 Thread Joel Goldstick
On Tue, Sep 9, 2014 at 10:02 AM, Sydney Shall wrote: > On 09/09/2014 15:44, Peter Otten wrote: > > Sydney Shall wrote: > > On 08/09/2014 18:39, Alan Gauld wrote: > > On 08/09/14 15:17, Juan Christian wrote: > > One tiny tweak... > > class User(): > > You don't need the parens after User. You don;t

Re: [Tutor] Good approach regarding classes attributes

2014-09-09 Thread Sydney Shall
On 09/09/2014 15:44, Peter Otten wrote: Sydney Shall wrote: On 08/09/2014 18:39, Alan Gauld wrote: On 08/09/14 15:17, Juan Christian wrote: One tiny tweak... class User(): You don't need the parens after User. You don;t have any superclasses so they do nothing. Python convention for an emp

Re: [Tutor] Good approach regarding classes attributes

2014-09-09 Thread Juan Christian
On Mon, Sep 8, 2014 at 5:58 AM, Peter Otten <__pete...@web.de > wrote: > > PS: This is not about being pythonic, but it might be more convenient for > client code if you use datetime objects instead of timestamps: > > >>> import datetime > >>> last_logoff = datetime.datetime.utcfromtimestamp(14100

Re: [Tutor] Good approach regarding classes attributes

2014-09-09 Thread Peter Otten
Sydney Shall wrote: > On 08/09/2014 18:39, Alan Gauld wrote: >> On 08/09/14 15:17, Juan Christian wrote: >> >> One tiny tweak... >> >>> class User(): >> >> You don't need the parens after User. You don;t have any superclasses >> so they do nothing. Python convention for an empty parent list is jus

Re: [Tutor] Good approach regarding classes attributes

2014-09-09 Thread leam hall
On Tue, Sep 9, 2014 at 9:09 AM, Sydney Shall wrote: > And while I am writing, what does OP stand for in this list? "Original Poster". So I understand. Won't answer the Python question since I'm a newbie here myself. -- Mind on a Mission ___ Tutor mai

Re: [Tutor] Good approach regarding classes attributes

2014-09-09 Thread Sydney Shall
On 08/09/2014 18:39, Alan Gauld wrote: On 08/09/14 15:17, Juan Christian wrote: One tiny tweak... class User(): You don't need the parens after User. You don;t have any superclasses so they do nothing. Python convention for an empty parent list is just to leave the parens off: class User

Re: [Tutor] Good approach regarding classes attributes

2014-09-08 Thread Peter Otten
Juan Christian wrote: >> >> > On Mon, Sep 8, 2014 at 5:58 AM, Peter Otten <__pete...@web.de> wrote: >> >> In that spirit here's an alternative implementation of the User class: >> >> from collections import namedtuple >> User = namedtuple( >> "User", >> "steamid personaname lastlogoff prof

Re: [Tutor] Good approach regarding classes attributes

2014-09-08 Thread Juan Christian
> > > On Mon, Sep 8, 2014 at 5:58 AM, Peter Otten <__pete...@web.de> wrote: > > In that spirit here's an alternative implementation of the User class: > > from collections import namedtuple > User = namedtuple( > "User", > "steamid personaname lastlogoff profileurl " > "avatar timecreat

Re: [Tutor] Good approach regarding classes attributes

2014-09-08 Thread Alan Gauld
On 08/09/14 15:17, Juan Christian wrote: 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

Re: [Tutor] Good approach regarding classes attributes

2014-09-08 Thread Alan Gauld
On 08/09/14 15:17, Juan Christian wrote: One tiny tweak... class User(): You don't need the parens after User. You don;t have any superclasses so they do nothing. Python convention for an empty parent list is just to leave the parens off: class User: -- Alan G Author of the Learn to Prog

Re: [Tutor] Good approach regarding classes attributes

2014-09-08 Thread Peter Otten
Juan Christian wrote: > On Mon, Sep 8, 2014 at 5:58 AM, Peter Otten <__pete...@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 i

Re: [Tutor] Good approach regarding classes attributes

2014-09-08 Thread Juan Christian
On Mon, Sep 8, 2014 at 5:58 AM, Peter Otten <__pete...@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 m

Re: [Tutor] Good approach regarding classes attributes

2014-09-08 Thread Peter Otten
Juan Christian wrote: > I'll definitely use the '@property' decoration. Thanks for the tip, Personally I'd use normal attributes, though. > so, a > different module to accommodate all the API requests and one for the > logic/code itself is a better approach, right? A separate function or meth

Re: [Tutor] Good approach regarding classes attributes

2014-09-07 Thread Alan Gauld
On 08/09/14 03:31, Juan Christian wrote: @property def steamid(self): return self._steamid Unless you specifically *need* these fields to be read-only you don't need the property declarations. Just use the _XXX convention to signal that they are *intended* to be private and allow clien

Re: [Tutor] Good approach regarding classes attributes

2014-09-07 Thread Danny Yoo
@property def _avatar(self): return self._avatar >> >> Hi Joel, >> >> The above code looks strange to me. The method and the field name >> should not use the same name. > > ah! good catch Danny. I didn't write it, I was commenting on the OP code. > > But (and maybe this was discuss

Re: [Tutor] Good approach regarding classes attributes

2014-09-07 Thread Joel Goldstick
On Sun, Sep 7, 2014 at 11:01 PM, Danny Yoo wrote: >>> @property >>> def _avatar(self): >>> return self._avatar > > Hi Joel, > > The above code looks strange to me. The method and the field name > should not use the same name. ah! good catch Danny. I didn't write it, I was commenting on the OP c

Re: [Tutor] Good approach regarding classes attributes

2014-09-07 Thread Danny Yoo
>> @property >> def _avatar(self): >> return self._avatar Hi Joel, The above code looks strange to me. The method and the field name should not use the same name. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: ht

Re: [Tutor] Good approach regarding classes attributes

2014-09-07 Thread Joel Goldstick
On Sun, Sep 7, 2014 at 10:31 PM, Juan Christian 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

Re: [Tutor] Good approach regarding classes attributes

2014-09-07 Thread Juan Christian
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 = lastl

Re: [Tutor] Good approach regarding classes attributes

2014-09-07 Thread Steven D'Aprano
On Mon, Sep 08, 2014 at 04:01:01AM +1000, Steven D'Aprano wrote a bunch of stuff about the User class... Ah, sorry guys, I did *not* intend to send that post. It's probably a bit incoherent, and certainly unfinished. I hit the wrong key and my mail program sent it. If I get time to finish it

Re: [Tutor] Good approach regarding classes attributes

2014-09-07 Thread Steven D'Aprano
On Sun, Sep 07, 2014 at 12:00:15AM -0300, 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 thin

Re: [Tutor] Good approach regarding classes attributes

2014-09-07 Thread Juan Christian
> > > On Sun, Sep 7, 2014 at 5:04 AM, Peter Otten <__pete...@web.de> wrote: > > > 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

Re: [Tutor] Good approach regarding classes attributes

2014-09-07 Thread Peter Otten
Juan Christian wrote: > 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 __ini

Re: [Tutor] Good approach regarding classes attributes

2014-09-07 Thread Juan Christian
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

Re: [Tutor] Good approach regarding classes attributes

2014-09-07 Thread Peter Otten
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:

Re: [Tutor] Good approach regarding classes attributes

2014-09-07 Thread Alan Gauld
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://

Re: [Tutor] Good approach regarding classes attributes

2014-09-06 Thread Juan Christian
Ops, sorry. Pastebin @ line 9: It's [JSON response lastlogoff] On Sun, Sep 7, 2014 at 12:00 AM, 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. r

[Tutor] Good approach regarding classes attributes

2014-09-06 Thread Juan Christian
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 Is it a good approac