[Tutor] Good approach regarding classes attributes

Peter Otten __peter__ at web.de
Tue Sep 9 15:44:22 CEST 2014


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 just
>> to leave the parens off:
>>
>> class User:
>>
> A simple question from a newbie, in response to this surprise.
> 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

and

class C(object): 
    pass

are the same, so there is no point adding the explicit object inheritance.

In Python 2 however

class C: 
    pass

will create a "classic class" whereas

class C(object): # preferred in Python 2
    pass

is a "newstyle class". The most notable difference between these is that 
properties work correctly only with newstyle classes. Therefore making all 
your classes "newstyle" is a good idea.

> And while I am writing, what does OP stand for in this list?

Original Poster, as Leam says. 




More information about the Tutor mailing list