[Tutor] __init__

Albert-Jan Roskam sjeik_appie at hotmail.com
Mon Sep 5 08:24:50 EDT 2016


From: Tutor <tutor-bounces+sjeik_appie=hotmail.com at python.org> on behalf of Steven D'Aprano <steve at pearwood.info>
Sent: Thursday, September 1, 2016 1:24 AM
To: tutor at python.org
Subject: Re: [Tutor] __init__
    
On Wed, Aug 31, 2016 at 06:35:44PM +0000, Albert-Jan Roskam wrote:
> 
> >In Python 3, old-style classes are gone. Even in Python 2, they're 
> >discouraged.
> 
> This suggests that old-style classes are still used, even in Python 3, doesn't it?
> 
> albertjan at debian:~$ python3.5
> Python 3.5.0 (default, Apr 13 2016, 20:39:27) 
> [GCC 4.9.2] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import re, inspect
> >>> re.findall("class.+:", inspect.getsource(tkinter))
> ['class Event:', 'class Variable:', 'class StringVar(Variable):', 'class IntVar(Variable):', 'class DoubleVar(Variable):', 'class BooleanVar(Variable):', 'class Misc:', 'className):', 'class(self):', 'class(self, className, sequence=None, func=None, add=None):',  'class(self, className, sequence):', 'class CallWrapper:', 'class XView:', 'class YView:', 'class Wm:', 'class Tk(Misc, Wm):', 'className):', 'class_tcl):', 'class_py):', "className='Tk', useTk=0):", 'class Pack:', 'class Place:', 'class Grid:', 'class BaseWidget(Misc):',  'classes:', 'classes:', 'class Widget(BaseWidget, Pack, Place, Grid):', 'class Toplevel(BaseWidget, Wm):', 'class Button(Widget):', 'class Canvas(Widget, XView, YView):', 'class Checkbutton(Widget):', 'class Entry(Widget, XView):', 'class Frame(Widget):',  "class_' in cnf:", "class' in cnf:", 'class Label(Widget):', 'class Listbox(Widget, XView, YView):', 'class Menu(Widget):', 'class Menubutton(Widget):', 'class Message(Widget):', 'class Radiobutton(Widget
 ):', 'class Scale(Widget):', 'class Scrollbar(Widget):', 'class Text(Widget, XView, YView):', 'class _setit:', 'class OptionMenu(Menubutton):', 'class Image:', 'class PhotoImage(Image):', 'class BitmapImage(Image):', 'class Spinbox(Widget, XView):', 'class  LabelFrame(Widget):', 'class PanedWindow(Widget):']


No. A bare class with no declared parent has object automatically added, 
in Python 3.

Compare the output of this:

class K:
    pass

print(K.__bases__)


in Python 2 and Python 3, and you will see () in Python 2 (an empty 
tuple, i.e. no bases classes) but (<class 'object'>,) in Python 3.


=====> Aha, thank you, I did not know that. So for Python-3-only code, the idiom is "class SomeClass:", and for Python2/3 code "class SomeClass(object)".
What are the most common code changes a developer needs to make when switching from old- to new-style classes?



> I sometimes want to use the @property decorator in classes that 
> inherit from e.g. tkinter.Frame. The getter then works, but the setter 
> fails without any sign. So I then also inherit from object, as in
> 
> class WickedFrame(tkinter.frame, object):
[...]
> I do this in Python 2.7. Is this the recommended approach? I also like the fact that I can use super() that way.

I believe that is fine. 


===> thanks again :-)


    


More information about the Tutor mailing list