Posts: 4 Threads: 2 Joined: Apr 2017 Hello, I've stumbled across a problem when creating a program. The start to my program looks along the lines of: def parseVote(s): if s == []: print('0') If the user doesn't input a value for parseVote() I want it to print '0' but it is missing an argument so wont do that, help would be greatly appreciated! Posts: 8,197 Threads: 162 Joined: Sep 2016 Apr-27-2017, 07:26 AM (This post was last modified: Apr-27-2017, 07:26 AM by buran.) You need to make the argument optional, i.e. provide default value: def parseVote(s=None): if not s: # no argument or 0 or empty list/tuple as argument print(0) else: print(s) In this example the default value is None, but it can be some number, e.g. 0, which will simplify the code. def parseVote(s=0): print(s) # print 0 if no argument but prints empty list/tuple if supplied as argument Note that you should not supply mutable default arguments, e.g. def parseVote(s=[]): THAT'S NOT THE RIGHT WAY Posts: 4 Threads: 2 Joined: Apr 2017 You don't know how long I have messed around with this, thank you God! Posts: 566 Threads: 10 Joined: Apr 2017 (Apr-27-2017, 07:26 AM)buran Wrote: Note that you should not supply mutable default arguments, e.g. def parseVote(s=[]): THAT'S NOT THE RIGHT WAY Actually, in some very specific cases this is the right way - if you build a caching mechanism. Though in those cases I would use underscored name. Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
Posts: 2,952 Threads: 48 Joined: Sep 2016 Posts: 566 Threads: 10 Joined: Apr 2017 What do you know - I got something right Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
Posts: 8,197 Threads: 162 Joined: Sep 2016 Yes, but that's very specific case, when this is actually desired result (i.e. you know what you are doing) and this is also mentioned in the link I provided - Quote:When the Gotcha Isn’t a Gotcha Sometimes you can specifically “exploit” (read: use as intended) this behavior to maintain state between calls of a function. This is often done when writing a caching function. Posts: 566 Threads: 10 Joined: Apr 2017 (Apr-27-2017, 11:56 AM)buran Wrote: Yes, but that's very specific case, when this is actually desired result (i.e. you know what you are doing) and this is also mentioned in the link I provided - I followed the link after I replied..... Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
Posts: 2,342 Threads: 62 Joined: Sep 2016 (Apr-27-2017, 11:26 AM)volcano63 Wrote: Actually, in some very specific cases this is the right way - if you build a caching mechanism. Though in those cases I would use underscored name. I've only ever seen it as a gotcha, including for experienced Python programmers, and have never actually seen this used for caching or anything like that (and I'd be reluctant to accept it as a good use case). Posts: 566 Threads: 10 Joined: Apr 2017 Apr-28-2017, 03:31 AM (This post was last modified: Apr-28-2017, 03:32 AM by volcano63.) (Apr-27-2017, 05:02 PM)micseydel Wrote: I've only ever seen it as a gotcha, including for experienced Python programmers, and have never actually seen this used for caching or anything like that (and I'd be reluctant to accept it as a good use case). Well, I have some experience, and though I've never seen it used by anyone else, I used it a couple of time. Does improving performance by 100-s of percents in resource-hogging time-critical apps count as a good cause(yes, occasionally improving performance counts, even in Python  ). Of course, you have to keep in mind - Cache maintenance - you may easily end up with memory leak
- You cannot re-assign cache - you may clean it, but you must preserve the reference
I am not sure if 4 years of Python (first time I used this technique couple of years ago) would define me as experienced Python programmer  , but I have some experience with programming... Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
|