empty arguments - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: empty arguments (/thread-3062.html) Pages:
1
2
|
empty arguments - noob - Apr-27-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! RE: empty arguments - buran - Apr-27-2017 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 argumentNote that you should not supply mutable default arguments, e.g. def parseVote(s=[]):THAT'S NOT THE RIGHT WAY RE: empty arguments - noob - Apr-27-2017 You don't know how long I have messed around with this, thank you God! RE: empty arguments - volcano63 - Apr-27-2017 (Apr-27-2017, 07:26 AM)buran Wrote: Note that you should not supply mutable default arguments, e.g. 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. RE: empty arguments - wavic - Apr-27-2017 *args ? RE: empty arguments - volcano63 - Apr-27-2017 What do you know - I got something right RE: empty arguments - buran - Apr-27-2017 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 RE: empty arguments - volcano63 - Apr-27-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..... RE: empty arguments - micseydel - Apr-27-2017 (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). RE: empty arguments - volcano63 - Apr-28-2017 (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
experienced Python programmer , but I have some experience with programming...
|