Passing an argument by reference

This is the place for queries that don't fit in any of the other categories.

Passing an argument by reference

Postby dday9 » Mon Oct 28, 2013 6:35 pm

I'm looking for a way to pass an argument in a function by reference. What I mean by this is something along the lines of:

Code: Select all
def set_arg(i)"
    i = 1
    return True


Then you'd call the function like this:
Code: Select all
if set_arg(i) == True:
    #...


Really what I'm wanting to do is something like this:
Code: Select all
def ParseInteger(i):
    try:
        i = int(i)
        return true
    except ValueError:
        i = None
        return false


How would I go about doing that?
dday9
 
Posts: 17
Joined: Fri Oct 11, 2013 8:55 pm

Re: Passing an argument by reference

Postby micseydel » Mon Oct 28, 2013 6:41 pm

In Python we prefer that functions which don't need to have side effects don't, so you can't actually do what you want. The closest thing is globals, but there are issues with that you're probably aware of. What you should do instead is something like this
Code: Select all
def parse_integer(i):
    try:
        return int(i)
    except ValueError:
        return None

my_integer = parse_integer(raw_input())
if my_integer is None:

and you can probably fill in the lines from there. This should be quite similar to what you want, but you check for None instead of True or False.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1214
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Passing an argument by reference

Postby dday9 » Mon Oct 28, 2013 6:42 pm

Alrighty, thanks for your reply.
dday9
 
Posts: 17
Joined: Fri Oct 11, 2013 8:55 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 1 guest