Re: Emulating Final classes in Python

2017-01-18 Thread Steve D'Aprano
On Thu, 19 Jan 2017 09:02 am, Ethan Furman wrote: [...] > One problem with the above is existing instances won't be modified to > inherit from the updated class. I am unsure if that is solvable before > 3.6, but in 3.6 one can use the new __init_subclass__ to avoid a Final > base class, a FinalMe

Re: Emulating Final classes in Python

2017-01-18 Thread Ethan Furman
On 01/18/2017 08:24 AM, Ethan Furman wrote: On 01/17/2017 11:05 PM, Steven D'Aprano wrote: I've given a metaclass that disallows subclassing: class MyClass(MyParent, metaclass=FinalMeta): ... Ethan took that one step further by giving a class you inherit from to disallow subclassing:

Re: Emulating Final classes in Python

2017-01-18 Thread Ethan Furman
On 01/17/2017 11:05 PM, Steven D'Aprano wrote: I've given a metaclass that disallows subclassing: class MyClass(MyParent, metaclass=FinalMeta): ... Ethan took that one step further by giving a class you inherit from to disallow subclassing: class MyClass(MyParent, Final): ... Cou

Re: Emulating Final classes in Python

2017-01-18 Thread alister
On Wed, 18 Jan 2017 13:10:41 +1100, Steven D'Aprano wrote: > On Tuesday 17 January 2017 20:37, Antoon Pardon wrote: > >> Op 17-01-17 om 08:05 schreef Steven D'Aprano: >>> I wish to emulate a "final" class using Python, similar to bool: >>> >>> py> class MyBool(bool): >>> ... pass ... >>> Trac

Re: Emulating Final classes in Python

2017-01-17 Thread Steven D'Aprano
On Wednesday 18 January 2017 15:42, Ethan Furman wrote: [...] > Both those cases are good candidates for disallowing subclassing. I've given a metaclass that disallows subclassing: class MyClass(MyParent, metaclass=FinalMeta): ... Ethan took that one step further by giving a class you inhe

Re: Emulating Final classes in Python

2017-01-17 Thread Ethan Furman
On 01/17/2017 01:37 AM, Antoon Pardon wrote: Op 17-01-17 om 08:05 schreef Steven D'Aprano: I wish to emulate a "final" class using Python, similar to bool: py> class MyBool(bool): ... pass ... Traceback (most recent call last): File "", line 1, in TypeError: type 'bool' is not an acce

Re: Emulating Final classes in Python

2017-01-17 Thread Steven D'Aprano
On Tuesday 17 January 2017 20:37, Antoon Pardon wrote: > Op 17-01-17 om 08:05 schreef Steven D'Aprano: >> I wish to emulate a "final" class using Python, similar to bool: >> >> py> class MyBool(bool): >> ... pass >> ... >> Traceback (most recent call last): >> File "", line 1, in >> TypeErr

Re: Emulating Final classes in Python

2017-01-17 Thread Steve D'Aprano
On Wed, 18 Jan 2017 06:14 am, Ethan Furman wrote: > On 01/16/2017 11:32 PM, Steven D'Aprano wrote: >> On Tuesday 17 January 2017 18:05, Steven D'Aprano wrote: >> >>> I wish to emulate a "final" class using Python, similar to bool: >> >> I may have a solution: here's a singleton (so more like None

Re: Emulating Final classes in Python

2017-01-17 Thread Steve D'Aprano
On Tue, 17 Jan 2017 08:54 pm, Erik wrote: > Hi Steven, > > On 17/01/17 07:05, Steven D'Aprano wrote: >> I wish to emulate a "final" class using Python, similar to bool: > > [snip] > >> It doesn't have to be absolutely bulletproof, but anyone wanting to >> subclass my class should need to work f

Re: Emulating Final classes in Python

2017-01-17 Thread Ethan Furman
On 01/16/2017 11:32 PM, Steven D'Aprano wrote: On Tuesday 17 January 2017 18:05, Steven D'Aprano wrote: I wish to emulate a "final" class using Python, similar to bool: I may have a solution: here's a singleton (so more like None than bools) where instantiating the class returns the singleton

Re: Emulating Final classes in Python

2017-01-17 Thread Erik
Hi Steven, On 17/01/17 07:05, Steven D'Aprano wrote: I wish to emulate a "final" class using Python, similar to bool: [snip] It doesn't have to be absolutely bulletproof, but anyone wanting to subclass my class should need to work for it, which hopefully will tell them that they're doing som

Re: Emulating Final classes in Python

2017-01-17 Thread Antoon Pardon
Op 17-01-17 om 08:05 schreef Steven D'Aprano: > I wish to emulate a "final" class using Python, similar to bool: > > py> class MyBool(bool): > ... pass > ... > Traceback (most recent call last): > File "", line 1, in > TypeError: type 'bool' is not an acceptable base type > > > It doesn't h

Re: Emulating Final classes in Python

2017-01-16 Thread Steven D'Aprano
On Tuesday 17 January 2017 18:05, Steven D'Aprano wrote: > I wish to emulate a "final" class using Python, similar to bool: [...] > Any hints? I may have a solution: here's a singleton (so more like None than bools) where instantiating the class returns the singleton, and subclassing the class f

Re: Emulating Final classes in Python

2017-01-16 Thread Ethan Furman
On 01/16/2017 11:05 PM, Steven D'Aprano wrote: I wish to emulate a "final" class using Python, similar to bool: py> class MyBool(bool): ... pass ... Traceback (most recent call last): File "", line 1, in TypeError: type 'bool' is not an acceptable base type It doesn't have to be absolu

Emulating Final classes in Python

2017-01-16 Thread Steven D'Aprano
I wish to emulate a "final" class using Python, similar to bool: py> class MyBool(bool): ... pass ... Traceback (most recent call last): File "", line 1, in TypeError: type 'bool' is not an acceptable base type It doesn't have to be absolutely bulletproof, but anyone wanting to subclass