git.net

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

class definition question


On 07Aug2019 16:36, Terry Reedy <tjreedy at udel.edu> wrote:
>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:
>>>>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.

By contrast, I generally use MyClass(object) because I try to write 
Python 2/3 compatible code unless I have a reason not to.

If I _know_ I'll never use a module with Python 2, the bare MyClass: is 
indeed preferable. However, a lot of my code is in modules which might 
conceivably be used by anyone. Artificially contraining their use seems 
counterproductive.

Cheers,
Cameron Simpson <cs at cskk.id.au>