osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

class definition question


On 8/7/2019 3:26 PM, Manfred Lotz wrote:
> On Wed, 07 Aug 2019 14:39:00 -0400
> Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
> 
>> On Wed, 7 Aug 2019 20:11:15 +0200, Manfred Lotz <ml_news at posteo.de>
>> declaimed the following:
>>
>>> Hi there,
>>> More often I see something like this:
>>>
>>> class Myclass:
>>> ...
>>>
>>>
>>> but sometimes I see
>>>
>>> class Myclass(object):
>>> ...
>>>
>>>
>>> Question: which way is preferable?
>>
>> 	It's historical...
>>
>> 	Python v1.x had a form of classes.
>>
>> 	Python v2.x introduced "new-style" classes. "New-style"
>> classes /had/ to inherit from "object", as they had different
>> behavior from "v1.x old-style" classes which were still supported (it
>> would have broken too many programs). Old-style were then deprecated,
>> and one should have used new-style for new code.
>>
>> 	Python v3.x unified (removed old-style behavior differences)
>> and all classes inherit from "object" whether one specifies object or
>> not.
>>
>>
> 
> Thanks a lot for the explanations.
> 
> As a Python newbie (with no Pythons legacies) I only deal with Python 3.
> So, I will happily ignore 'object'.

That is preferred, because it is extra work to write and read, with no 
benefit, and because myclass(object) can be seen as implying that the 
code once ran or is still meant to be compatible with Python 2.


-- 
Terry Jan Reedy