Hello list,
I was on this list years ago, using a different email address. I'm now
working for a company where, among other things, I find myself needing
to create a GUI application to let us manage and run server jobs. It's
going pretty well, but a couple parts of WX are tripping me up. I'll
start with what I hope is an easy one: subclassing wx.Frame.
In every example I can find online, you do it like this:
class MyFrame(wx.Frame):
def __init__(self, parent, title="My Application", size=(480, 360):
wx.Frame.__init__(parent, title, size) #keep doing your own initializing
I'm doing this from memory, so I might have a couple arguments wrong.
The main thing of note is the call to wx.Frame.__init__. In most
subclasses, you'd do it this way:
What I'm not sure of is why you use the wx.Frame.__init__ directly,
instead of using super. There's probably a good reason, but I'm coming
back to Python/WX after a couple years of playing with Swift/ObjC and
Cocoa on a Mac, so I'm just not seeing the obvious answer. I'll have
more questions, but I want to keep each in its own thread. Thanks for
any answers anyone can offer.
In every example I can find online, you do it like this:
class MyFrame(wx.Frame):
def __init__(self, parent, title="My Application", size=(480, 360):
wx.Frame.__init__(parent, title, size) #keep doing your own initializing
I'm doing this from memory, so I might have a couple arguments wrong.
The main thing of note is the call to wx.Frame.__init__. In most
subclasses, you'd do it this way:
What I'm not sure of is why you use the wx.Frame.__init__ directly,
instead of using super.
super was added in Python 2.2, and only works for new-style classes. As
an awful lot of wxPython documentation is quite dated now, my guesses
would be that:
1. super wasn't a part of the language when the example(s) or
tutorial(s) were written;
2. The example(s) or tutorial(s) were written at a time that super was
available, but people were used to using old-style classes; or
3. The authors of newer tutorials and samples weren't aware of, or in
the habbit of using, super in their subclasses. If the tutorial was
written by or for beginners and penned somewhat recently, this seems
like the most likely answer. When I was a beginner, writing my first
wxPython code, I didn't know what super was or what it did, and the
official documentation would've probably confused me a fair bit. So the
fact that the tutorials I was using avoided its use probably helped me
get to grips with the content with as few stumbling blocks as possible.
Thanks for the answer. Can I safely use super(myClass,
self).__init__(...) in my code? It just makes more sense to me to do
it that way, probably because of Swift and ObjC doing that.
···
On 1/28/16, James Scholes <james@jls-radio.com> wrote:
Alex Hall wrote:
In every example I can find online, you do it like this:
class MyFrame(wx.Frame):
def __init__(self, parent, title="My Application", size=(480, 360):
wx.Frame.__init__(parent, title, size) #keep doing your own initializing
I'm doing this from memory, so I might have a couple arguments wrong.
The main thing of note is the call to wx.Frame.__init__. In most
subclasses, you'd do it this way:
What I'm not sure of is why you use the wx.Frame.__init__ directly,
instead of using super.
super was added in Python 2.2, and only works for new-style classes. As
an awful lot of wxPython documentation is quite dated now, my guesses
would be that:
1. super wasn't a part of the language when the example(s) or
tutorial(s) were written;
2. The example(s) or tutorial(s) were written at a time that super was
available, but people were used to using old-style classes; or
3. The authors of newer tutorials and samples weren't aware of, or in
the habbit of using, super in their subclasses. If the tutorial was
written by or for beginners and penned somewhat recently, this seems
like the most likely answer. When I was a beginner, writing my first
wxPython code, I didn't know what super was or what it did, and the
official documentation would've probably confused me a fair bit. So the
fact that the tutorials I was using avoided its use probably helped me
get to grips with the content with as few stumbling blocks as possible.
--
James Scholes x.com
--
You received this message because you are subscribed to the Google Groups
"wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
yes, you sure can -- super() is only really potentially different when you
are doing multiple inheritance.
BTW, if you are going to be "modern" about it, use *args, **kwargs whenever
you can:
class MyFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
(I still kind of like the explicitness of calling the __init__ directly...)
now you have your version of the Frame that take all the same arguments as
the regular one...
-CHB
···
On Thu, Jan 28, 2016 at 9:26 AM, Alex Hall <ahall@autodist.com> wrote:
Thanks for the answer. Can I safely use super(myClass,
self).__init__(...) in my code?
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
An even more pythonic (though more obscure) way to do this is with
super(self.class,self).init(*args, **kwargs)
I know we’re straying a bit OT again, but I just have to say: that’s really COOL! I always forget about the built-in * attributes, and as you said, class will let you not care what you name your subclass. Now I just have to figure out how *args and **kwargs work if functions that take them don’t appear to unpack/iterate over them, and I’ll be set. Off to Google I go.