0

I have read that while writing functions it is good practice to copy the arguments into other variables because it is not always clear whether the variable is immutable or not. [I don't remember where so don't ask]. I have been writing functions according to this.

As I understand creating a new variable takes some overhead. It may be small but it is there. So what should be done? Should I be creating new variables or not to hold the arguments?

I have read this and this. I have confusion regarding as to why float's and int's are immutable if they can be changed this easily?

EDIT:

I am writing simple functions. I'll post example. I wrote the first one when after I read that in Python arguments should be copied and the second one after I realized by hit-and-trial that it wasn't needed.

#When I copied arguments into another variable
def zeros_in_fact(num):
    '''Returns the number of zeros at the end of factorial of num'''
    temp = num
    if temp < 0:
        return 0
    fives = 0
    while temp:
        temp /=  5
        fives += temp
    return fives

#When I did not copy arguments into another variable
def zeros_in_fact(num):
    '''Returns the number of zeros at the end of factorial of num'''
    if num < 0:
        return 0
    fives = 0
    while num:
        num /=  5
        fives += num
    return fives
7
  • The questions you linked to (particularly the first one) show that floats and ints cannot be changed. Also, it is not good practice to unnecessarily copy function arguments. Can you give an example of the kind of code you're writing?
    – BrenBarn
    Jun 18, 2013 at 6:06
  • The question's first answer shows how easily int's and float's can be changed. Obviously they can be changed otherwise what would be their use? Jun 18, 2013 at 6:09
  • @Zel Wrong. Each integer is an immutable object. Each float is an immutable object. Operations on immutable numbers return new immutable numbers. Assigning an immutable number to a mutable variable (reference to an object) mutates the reference to point to a new immutable number. Etc. The point is that you can never edit a number and have its changes be witnessed through two variables pointing to it, as numbers are immutable.
    – Patashu
    Jun 18, 2013 at 6:10
  • 1
    @Zel If you've ever programmed in Java or C# then you know that in those languages integers and floats are value-type primitives, that are copied by value not by reference, and when you change the value that's in an integer primitive variable the change is seen in no other variable. Making integers immutable in Python is an attempt to mimic this, similar to how Strings are immutable in Java/C# to mimic this - so that the value in a variable that is an integer or floating point cannot 'suddenly, unexpectedly change' through changes via another variable. This helps you reason about your program.
    – Patashu
    Jun 18, 2013 at 6:27
  • 1
    @Zel It's not to implement passing by value, it's to mimic the semantics of passing primitives by value when in reality you're passing (immutable) objects by reference. When you have mutable numbers passed by reference it is too easy to get confused over 'are these two 5s different 5s, or if I add 2 to one 5 both will become 7?' and it is a cause of a lot of bugs. So making number objects immutable is anti-bugs and pro-productivity.
    – Patashu
    Jun 18, 2013 at 6:36

3 Answers 3

3

I think it's best to keep it simple in questions like these.

The second link in your question is a really good explanation; in summary:

Methods take parameters which, as pointed out in that explanation, are passed "by value". The parameters in functions take the value of variables passed in.

For primitive types like strings, ints, and floats, the value of the variable is a pointer (the arrows in the following diagram) to a space in memory that represents the number or string.

code               | memory
                   |
an_int = 1         |    an_int ---->  1
                   |                  ^    
another_int = 1    |    another_int  /

When you reassign within the method, you change where the arrow points.

an_int = 2         |    an_int -------> 2
                   |    another_int --> 1

The numbers themselves don't change, and since those variables have scope only inside the functions, outside the function, the variables passed in remain the same as they were before: 1 and 1. But when you pass in a list or object, for example, you can change the values they point to outside of the function.

a_list = [1, 2, 3]    |              1   2   3
                      |   a_list ->| ^ | ^ | ^ |
                      |              0   2   3
a_list[0] = 0         |   a_list ->| ^ | ^ | ^ |

Now, you can change where the arrows in the list, or object, point to, but the list's pointer still points to the same list as before. (There should probably actually only be one 2 and 3 in the diagram above for both sets of arrows, but the arrows would have gotten difficult to draw.)

So what does the actual code look like?

a = 5
def not_change(a):
  a = 6
not_change(a)
print(a) # a is still 5 outside the function

b = [1, 2, 3]
def change(b):
  b[0] = 0
print(b) # b is now [0, 2, 3] outside the function

Whether you make a copy of the lists and objects you're given (ints and strings don't matter) and thus return new variables or change the ones passed in depends on what functionality you need to provide.

2

What you are doing in your code examples involves no noticeable overhead, but it also doesn't accomplish anything because it won't protect you from mutable/immutable problems.

The way to think about this is that there are two kinds of things in Python: names and objects. When you do x = y you are operating on a name, attaching that name to the object y. When you do x += y or other augmented assignment operators, you also are binding a name (in addition to doing the operation you use, + in this case). Anything else that you do is operating on objects. If the objects are mutable, that may involve changing their state.

Ints and floats cannot be changed. What you can do is change what int or float a name refers to. If you do

x = 3
x = x + 4

You are not changing the int. You are changing the name x so that it now is attached to the number 7 instead of the number 3. On the other hand when you do this:

x = []
x.append(2)

You are changing the list, not just pointing the name at a new object.

The difference can be seen when you have multiple names for the same object.

>>> x = 2
>>> y = x
>>> x = x + 3 # changing the name
>>> print x
5
>>> print y # y is not affected
2
>>> x = []
>>> y = x
>>> x.append(2) # changing the object
>>> print x
[2]
>>> print y # y is affected
[2]

Mutating an object means that you alter the object itself, so that all names that point to it see the changes. If you just change a name, other names are not affected.

The second question you linked to provides more information about how this works in the context of function arguments. The augmented assignment operators (+=, *=, etc.) are a bit trickier since they operate on names but may also mutate objects at the same time. You can find other questions on StackOverflow about how this works.

1

If you are rebinding the name then mutability of the object it contains is irrelevant. Only if you perform mutating operations must you create a copy. (And if you read between the lines, that indirectly says "don't mutate objects passed to you".)

4
  • What exactly does mutating mean? As I understand if there is string abc then abc[0] = 'a' is a mutating operation for string abc. Am I correct? Is yes, then can you add some mutating operations for int's and float's? Will bit-wise operators be considered as mutating operatins on int's and float's? Jun 18, 2013 at 6:18
  • str, unicode, int, float, complex, tuple, and FrozenSet have no mutating operations. Jun 18, 2013 at 6:20
  • @IgnacioVazquez-Abrams There are no valid mutating operations but what I have given is an example of what could be considered as an invalid try to mutate the immutable str. Although it is invalid it is a try to mutate the str. I understand that as they are immutable there can be no mutating operations possible. But for the sake of understanding what is mutating can you give you an example of invalid mutating operations on these immutable data types that would fail? Jun 18, 2013 at 6:25
  • 2
    int, float, and complex have no way to even attempt to mutate them; all augmented assignment operations (+=, /=, etc.) return a new object and bind it to the name. Jun 18, 2013 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.