Preferred style

51 views
Skip to first unread message

Phil

unread,
Apr 8, 2021, 2:17:06 AM4/8/21
to wxpytho...@googlegroups.com
Thank you for reading this.

It's been quite some time since I've done anything with WxPython and
while looking through some of my old code for ideas I see that I have
copied many different styles over the years and now I'm wondering which
is preferred:

wx.Frame.__init__(self, None, wx.ID_ANY, "Pointer Test", size=(300,300))

or

super(Mywin, self).__init__(parent, title=title, size=(500, 400))

The WxPython books that I have are quite dated and I suppose super is
the more recent and the preferred method.

And what about this with the window name here instead of in the line
beginning with "super"?

app = wx.App()
Mywin(None, 'Name_of_window')
app.MainLoop()

--

Regards,
Phil

RF

unread,
Apr 21, 2021, 3:50:58 PM4/21/21
to wxPython-users
Hi Phil,

As a new Python coder, I'm struggling with coding format and structure also. When I look at coding examples, I see all kinds of formatting styles. I even see the same functions called with different number of arguments. Depends on what syntax you use. For a newbie it's extremely confusing. I'm sticking with using OOP format. It's a steep mountain to get over while learning Python but I think in the end, it will be worth the effort. A few light bulbs have gone off in my head as I work on my project and I'm slowly getting the big structural picture of how an OOP Python file works. Obviously, I have a long ways to go but I'm retired and have no time constraints so that is good.

Steve Barnes

unread,
Apr 22, 2021, 1:44:58 AM4/22/21
to wxpytho...@googlegroups.com

Hi,

 

Just a little bit of background knowledge.

 

Python functions and class member functions  can be defined and called with:

  • No arguments def fn(): or for a class member functions def fn(self): - in this case calling fn or c.fn with arguments is an error.
  • One or more arguments with no defaults: i.e.: def fn(a, b, c): or def fn(self, a, b, c): - in this case they must always be called with the exact number of arguments but these arguments can be given by position or name, e.g. fn(1, 2, 3) or fn(c=3, a=1, b=2)  or a mix fn(1, c=3, b=2) are all equivalent.
  • With some or all of the arguments given default values, e.g. def fn(a, b, c=0): or def fn(a=0, b=0, c=0): (but once you start giving default values you have to continue so def fn(a=0, b=0, c): is an error) in this case any missing parameters will be given the default value.
  • Hopefully by now you are picking up that there are 2 types of arguments (positional and keyword) - fundamentally when you call the function or any function these are passed to the function call as a list of positional arguments and a dictionary of keyword arguments. So sometimes functions with a lot of arguments are defined as def fn(*argc, **argv): which can be called as fn(1, 2, 3, x=17, y=22) - note that since the function definition doesn't specify names for the individual positional and keyword arguments it is down to the code in the function to validate the parameters and legal calls can include any number of positional arguments including none and any number of any names of keyword arguments.
  • Python 3 introduces (PEP-3102) the idea of Keyword only arguments - these are arguments must be supplied, (no defaults), and cannot be given by position.
  • Python 3.8 introduces (PEP-570) Position only arguments - these must be supplied (unless they have defaults) and cannot be called with a name.
  • There is also a sneaky trick that you can unpack a list of values (of the same length as the positional arguments) by using the * operator and a dictionary with keys the same as your keyword arguments using the ** operator. So you could see def fn(a, b, c=0): called with fn(*list_of_2_or_3_values) or fn(**dict_of_abc) or anything in between.
  • In addition there is the option of adding type hinting to your functions - this is used to tell the developer (and certain checking tools) the expected type(s) of input parameters and returned value(s).

 

I hope that the above (short and not 100% comprehensive but hopefully accurate helps to make things at least a little clearer,

 

I would encourage you to use pylint and possibly black these will help to keep your style consistent and valid but there are a number of other tools.

 

Steve Barnes.

--
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-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/811e41b1-b82b-4d8d-91ad-d20e7e4d6d98n%40googlegroups.com.

RF

unread,
Apr 22, 2021, 10:21:38 AM4/22/21
to wxPython-users
Thank you Steve. I installed pylint into my PyCharm IDE.

Phil

unread,
Apr 22, 2021, 9:04:24 PM4/22/21
to wxpytho...@googlegroups.com
On 22/4/21 3:44 pm, Steve Barnes wrote:
>
> Hi,
>
> I would encourage you to use pylint <https://www.pylint.org/> and
> possibly black <https://black.readthedocs.io/en/stable/> these will
> help to keep your style consistent and valid but there are a number of
> other tools.

Thank you Steve and RF for your replies,

My question didn't really relate to formatting but more to the use of
"super", default arguments and the use of -1 as an argument in the
constructor.  Your reply, Steve, has cleared up a couple of points that
I found confusing. More reading is required.

I have used several different linters, formatters and IDEs over the
years. I recently installed VS Code and like it a lot. I'm now using
Pylint and Yapf.

--

Regards,
Phil

Steve Barnes

unread,
Apr 23, 2021, 2:39:49 AM4/23/21
to wxpytho...@googlegroups.com
Hi Phil,

Glad if I helped. A couple of final points that may help.

The use of -1 by wx & wxPython in some places as a parameter - the most common place to see this is for the Windows ID - each window needs a unique ID and in the Windows world that ID is an Unisigned Long type so Zero is a valid ID (definitely not one that you want to use). Duplicating IDs causes all sort of problems. You can manually set your windows IDs to values in the User range and try to keep track of them yourself but that often results in clashes accordingly wx & wxpython use an absolute value of -1 to say "Please assign the next unused number in the permitted range" to wx - this can also be spelt wx.ID_ANY which is preferable from a maintenance point of view but is 9 keys rather than 2 - hence a lot of examples use -1. If you look at the signature of wx.Frame you will see Frame(parent, id=ID_ANY, title=EmptyString, pos=DefaultPosition, size=DefaultSize, style=DEFAULT_FRAME_STYLE, name=FrameNameStr) but if you have imported wx and just type wx.ID_ANY you will see an output of -1.

The use of super - this is used to access and call parent class methods usually those with the same name as a method that your class defines - most commonly the __init__ method but it can be used for any method - python 3 simplified the syntax for super, but still allows the older syntax so you will often see super(parent_class, self).__init__(....) in older or backwards compatible code rather than the newer super().__init__(...).

One of the joys of both Python and wx is the degree of backwards compatibility that the developers have managed to maintain but sometimes it does lead to outdated code examples, online and in books, that can cause confusion. Note that whenever I have found old code that is "broken" by an update and fix the issue usually it turns out to be that I was getting away with doing something that I shouldn't have been and the fixed code still works with the older versions of python & wx (but possibly not the very old versions).

All the best for your wxpython journey.

Steve

-----Original Message-----
From: wxpytho...@googlegroups.com <wxpytho...@googlegroups.com> On Behalf Of Phil
Sent: 23 April 2021 02:04
To: wxpytho...@googlegroups.com
Subject: Re: [wxPython-users] Re: Preferred style

On 22/4/21 3:44 pm, Steve Barnes wrote:
>
> Hi,
>
> I would encourage you to use pylint
> <https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fww
> w.pylint.org%2F&amp;data=04%7C01%7C%7C46ecf246d9bb4a69aa0708d905f3bcb0
> %7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637547366658419079%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=uzb6A8LVwEcrEU4JT692sfx%2BCVST0YsCdG4gQhxCMVU%3D&amp;reserved=0> and possibly black <https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fblack.readthedocs.io%2Fen%2Fstable%2F&amp;data=04%7C01%7C%7C46ecf246d9bb4a69aa0708d905f3bcb0%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637547366658419079%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=WqV2pJ6jX1PRg9taZvhXVW%2B%2Bd1SCNMA0tYKx4iiF9ZE%3D&amp;reserved=0> these will help to keep your style consistent and valid but there are a number of other tools.

Thank you Steve and RF for your replies,

My question didn't really relate to formatting but more to the use of "super", default arguments and the use of -1 as an argument in the constructor.  Your reply, Steve, has cleared up a couple of points that I found confusing. More reading is required.

I have used several different linters, formatters and IDEs over the years. I recently installed VS Code and like it a lot. I'm now using Pylint and Yapf.

--

Regards,
Phil

--
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-user...@googlegroups.com.
To view this discussion on the web visit https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fwxpython-users%2Fd207bacc-5e53-9add-57c9-3f0bedc40b5c%2540gmail.com&amp;data=04%7C01%7C%7C46ecf246d9bb4a69aa0708d905f3bcb0%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637547366658419079%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=qLTAT2IyYaqv2Ui9NELjHUsfA9iEEc9bROR5I7DhNqg%3D&amp;reserved=0.

Phil

unread,
Apr 24, 2021, 1:40:15 AM4/24/21
to wxpytho...@googlegroups.com
On 23/4/21 4:39 pm, Steve Barnes wrote:
> Hi Phil,
>
> Glad if I helped. A couple of final points that may help.

Thanks again for your detailed reply,

It was, or possibly still is, the outdated examples that caused the
confusion. I'm taking guidance from the Demo code.

--

Regards,
Phil

Chris Barker

unread,
Jul 3, 2021, 12:50:04 PM7/3/21
to wxpython-users
This:

is very old, but still helpful.

It would be really great if someone wants to update it for modern wx and modern style.

-CHB




--

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

Chris....@noaa.gov
Reply all
Reply to author
Forward
0 new messages