Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
empty arguments
#1
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!
Reply
#2
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
Reply
#3
You don't know how long I have messed around with this, thank you God!
Reply
#4
(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.
Reply
#5
*args ?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
What do you know - I got something right Tongue
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.
Reply
#7
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.
Reply
#8
(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.
Reply
#9
(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).
Reply
#10
(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   Tongue ).

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  Confused , but I have some experience with programming... Snooty
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names) Adelton 2 3,855 Mar-02-2017, 10:23 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020