RE: Re: Preferred style
Steve Barnes <[email protected]> Thu, 22 Apr 2021 05:44:53 +0000
| Newsgroups | gmane.comp.python.wxpython |
|---|---|
| Message-ID | <VI1PR03MB4479B045C94C1315A3C4E4509B469@VI1PR03MB4479.eurprd03.prod.outlook.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<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. Steve Barnes. From: [email protected] <[email protected]> On Behalf Of RF Sent: 21 April 2021 20:51 To: wxPython-users <[email protected]> Subject: [wxPython-users] Re: Preferred style 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. On Wednesday, April 7, 2021 at 11:17:06 PM UTC-7 Phil wrote: 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 -- 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 [email protected]<mailto:[email protected]>. To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/811e41b1-b82b-4d8d-91ad-d20e7e4d6d98n%40googlegroups.com<https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fwxpython-users%2F811e41b1-b82b-4d8d-91ad-d20e7e4d6d98n%2540googlegroups.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=04%7C01%7C%7Cc7fa6cab7c364c99843708d904fecd03%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637546314664179371%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=lP4fAPJwFz7Lx5ruI6fmnNlaVewVh%2FJELn7wxGLy%2FYk%3D&reserved=0>. -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/VI1PR03MB4479B045C94C1315A3C4E4509B469%40VI1PR03MB4479.eurprd03.prod.outlook.com.