1

I need to convince myself that this is a bad idea. I want to assign a second name to the same variable, whose contents may change.

A='Bob'
B=A #wrong function, I want to figure out something that works here.
A='Alice'
print A
print B

I want both A and B to print 'Alice' but instead B prints 'Bob' and A prints 'Alice'.

Why do I want to do this? In my code I am using one name for a bit of data, that makes sense for processing, but a different name makes sense for user input. argparse seems to want to assign a variable name based on the command line option. So I want to use one name when the data is entered, but another name when it is processed. I would prefer to avoid copying one to the other, but that is my fallback position.

Skip Huffman
  • 5,239
  • 11
  • 41
  • 51

4 Answers4

6

Python strings are immutable:

In Python the string object is immutable - each time a string is assigned to a variable a new object is created in memory to represent the new value.

Wikipedia doc on immutability and specific Python example.

One simple way to get the behavior you want is to wrap a string in a class thusly:

class MyClass:
    SomeStr = "Any Val"

A = MyClass
A.SomeStr = "Bob"
B=A
B.SomeStr = "Alice"

print(A.SomeStr)
print(B.SomeStr)

Output:

>>> 
Alice
Alice

So now your B=A assignment creates a "shallow" copy... The two variables, A & B point to the same object.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • @vzwick: Quite the contrary - *everything* is a pointer. It's just pointers to an objects only, and thus there are no pointers to pointers (which would be needed for this). –  Oct 17 '11 at 14:50
  • @delnan Thanks for the explaination, mate. Now I know for sure what language I'm never going to learn. – vzwick Oct 17 '11 at 14:52
  • 1
    And the key seems to be that when a variable's value is changed, the contents of the pointer are not changed, the variable is just assigned to a new pointer. (Well, the pointer is assigned to the variable.) – Skip Huffman Oct 17 '11 at 14:53
  • @vzwick: The idea of object immutability comes up in a lot of languages. Strings, and many other types, are immutable in Java and C#, just as a couple of example languages. It's best to learn the concept and spot in your language of choice because it will most likely exist whether you're aware of it right now or not. FYI: Javascript strings are immutable too: http://stackoverflow.com/questions/51185/are-javascript-strings-immutable-do-i-need-a-string-builder-in-js – Paul Sasik Oct 17 '11 at 15:05
  • 2
    This point of Python semantics is independent from the issue of object im-/mutability (although the two do interact). But it's still true that many other languages share this lack of explicit pointers. Off the top of my head, it's true for Lisp, Lua, Java, **JavaScript** (really, Python is identical to JS in this), Ruby, the non-`unsafe`/"managed" subset of C# (which is all most people need and use anyway). It should be noted that one can use objects with a single field to emulate pointers-to-pointers fairly easily. It should also be noted that few if any idiomatic code feels this is needed ;) –  Oct 17 '11 at 15:42
3

If you really want to be convinced that it's a bad idea, maybe for your argparse example you'd want to rename the variable being used to be differently from the option name. Use the dest option of argparse.add_argument

Only one of the examples from the doc:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', dest='bar')
>>> parser.parse_args('--foo XXX'.split())
Namespace(bar='XXX')
cfi
  • 10,915
  • 8
  • 57
  • 103
  • Ah! That is exactly what I need in this particular case. One upvote. (But since that isn't the question I asked, I am going to pick a different response as the "Answer". Sorry.) – Skip Huffman Oct 17 '11 at 15:23
2

I'd argue that the processing part should be a separate module from the input part, so neither should have any reason to know about the other. Input the data and store it in an appropriate place, and pass it to the processing part as a parameter using whatever name you want.

Jacob Mattison
  • 50,258
  • 9
  • 107
  • 126
2

You can only do this for values which Python consider variables to names of references to, e.g. lists, dictionaries, and so on.

A=["Bob"]
B=A
A[0] = "Alice"
print A, B

will print "Alice" twice.

unwind
  • 391,730
  • 64
  • 469
  • 606