[Twisted-Python] Inheriting DatagramProtocol, which is an old-style class

Go Luhng goluhng at gmail.com
Sun Feb 9 06:19:13 MST 2020


Thanks!  I will likely choose option 2, since I'm stuck on a very
large legacy pile of Python 2 code that I can't port up just yet.

On Sat, Feb 8, 2020 at 12:59 AM Tom Most <twm at freecog.net> wrote:
>
> Old-style classes remain old-style for compatibility reasons. You have some options, though:
>
> 1. Set the TWISTED_NEWSTYLE environment variable [1] to make all such old-style classes new-style.
> 2. When subclassing it, also subclass object, making the result new-style.
> 3. Switch to Python 3, where all-classes are new-style.
>
> Option 3 is the best, since we will drop Python 2.7 support soon enough. Option 1 may be helpful when migrating a codebase.
>
> ---Tom
>
> [1]: https://github.com/twisted/twisted/blob/c0a51509974e995537212efc5074140388585da6/src/twisted/python/compat.py#L61
>
> On Fri, Feb 7, 2020, at 9:44 PM, Maarten ter Huurne wrote:
> > On Saturday, 8 February 2020 01:50:04 CET Go Luhng wrote:
> > > I am creating a child class
> > > `Child(twisted.internet.protocol.DatagramProtocol)` to implement a
> > > custom UDP multicast protocol.
> > >
> > > I need to add an `__init__()` to `Child`, but as part of that I need
> > > to call `super()` which is impossible because `DatagramProtocol` is
> > > old-style.
> > >
> > > How should I proceed?
> >
> > There is no __init__() in DatagramProtocol or in its superclass
> > AbstractDatagramProtocol, so you can just skip the call.
> >
> > > Also, why are there old-style classes in the latest release of
> > > Twisted?
> >
> > I don't know the reason, but it seems to be deliberate, since
> > AbstractDatagramProtocol is annotated with @_oldStyle in the source.
> >
> > Bye,
> >               Maarten
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python



More information about the Twisted-Python mailing list