5

Possible Duplicate:
Python: How do I pass a variable by reference?

How can I reassign a value to a variable that is passed as a function argument?

For instance, what I am looking to do is:

foo = True

def convert(foo):
    if foo == True:
        foo = 'on'
    elif foo == False:
        foo = 'off'
    return foo

where foo is now a string. The problem with the method above is that in order to change foo from a boolean type to a string type the following must be passed:

foo = convert(foo)

whereas I am looking to do something like:

convert(foo)

or,

foo.convert()

Any ideas?

Community
  • 1
  • 1
BFTM
  • 3,225
  • 6
  • 23
  • 22
  • Sounds like you want this: http://stackoverflow.com/q/986006/21475. Also, worth reading about Python's `global` keyword (but it's not really applicable here). – Cameron Dec 07 '12 at 21:21
  • See Lev's answer about the cleanest (and most correct) solution. If you insist on being able to change the object without returning it, wrap it in something like a class or a dict (for example a simple hack would be to use foo={True} and pass that). – Bitwise Dec 07 '12 at 21:27

1 Answers1

8
foo = convert(foo)

is the cleanest and most explicit way to achive this. This is the way most people would recommend, if you are sure you even need to reassign a string to a bool variable.

foo.convert()

is something you can do on an instance of a class that you need to define. You can do it, but it's not worth the hassle. Just reassign the variable, that's it.

convert(foo) # actually, just convert()

can also work, but you'd have to use the global keyword in the convert function. This is not recommended, especially when it's so easy to avoid.

In [1]: foo = True

In [2]: def convert():
   ...:    global foo
   ...:    foo = 'on' if foo else 'off'
   ...:     

In [3]: convert()

In [4]: foo
Out[4]: 'on'
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
  • 1
    It's not only the cleanest, without some extremely messy hacking on the namespace that would be nearly impossible to make work for all use cases, it's impossible. As to the second, unless you did something like monkeypatching `convert()` on to `object` (which would be a very bad idea), also impossible. – Silas Ray Dec 07 '12 at 21:22
  • @sr2222 I'm not sure how to achieve this with monkey-patching either. I mean, the method can't reassign it's owner, right? But something can be done with defining a class, probably involving some changes in the rest of the code, though. Anyway, it's definitely a bad idea to even consider it. Edit: I didn't get that you meant `object`, not just "object". Still not sure how it can help, though. – Lev Levitsky Dec 07 '12 at 21:27
  • You could monkey patch `convert()` on to `object` itself, which would mean every object (at least new-style and most built in) that didn't have it's own `convert` would now have the `convert` method you defined. Since the type of `foo` is undefined, and in fact can't be defined, in the OP's scenario, the only way to add this even close to universally is to do a Very Bad Thing and directly screw with `object`. Even then though, it would be some very tricky code to actually push the converted value back to the label in whatever namespace `convert` was called in without an explicit assignment. – Silas Ray Dec 07 '12 at 21:38
  • 2
    It may be impossible to even do what we're talking about at all. Though given how much of a Very Bad Idea it would be, I'm not particularly inclined to try to figure it out. ;) – Silas Ray Dec 07 '12 at 21:42