**python : passing list as argument and updating in definition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting **python : passing list as argument and updating in definition
# 1  
Old 08-12-2015
**python : passing list as argument and updating in definition

In the below python code..
Could anyone please let me know why the name(variable) is getting modified if I update the kargs variable in the definition,


Code:
def f( kargs):
    kargs.extend([10])
    print ("In function :",kargs)

name = [1,2,3]
f(name)
print("Outside function :",name)

Output
Code:
>>> 
In function : [1, 2, 3, 10]
Outside function : [1, 2, 3, 10]
>>>

# 2  
Old 08-12-2015
Python doesn't make a copy of the array when passed into a function, it passes a reference to the same array. More fundamental types would get passed by value instead.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-12-2015
Because:
Code:
kargs.extend([10])

Which then changes name, which is displayed twice.

AFAIK: Python does preserve the value/content of variables among functions of the same file.
So changing it once, will effect all output of name, unless you set it back to its original value, before extending it, using a 2nd variable.

hth
# 4  
Old 08-12-2015
But logically it is going to be wrong if I am not wrong.Is there a way to correct this one.

Logically speaking ... name and kargs are two different variables referring the same namespace.In our case .. since I am extending the local variable inside the function it should be local to the function itself.

But why it is effected the actual value.

Thanks much for reverting me for my question.
# 5  
Old 08-12-2015
Because it actually holds an object ID or something like it.
# 6  
Old 08-12-2015
In Python the assignment operator `=' never copies data. Whatever is at the left of the `=' is a binding label to a value, not a variable per se. This is significant, since moving labels do not produce another variable.

I recommend you watch Ned Batchelder - Facts and Myths about Python names and values - PyCon 2015 if you are interested on it.
At 15:50 you'll get presentation related to your query, but I would watch it from the beginning since the concept of binding and rebinding labels builds from understanding mutable and immutable values.

Hope it helps.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing a second argument

I am trying to pass a second argument like so: if ] then export ARG2=$2 else message "Second argument not specified: USAGE - $PROGRAM_NAME ARG1 ARG2" checkerror -e 2 -m "Please specify if it is a history or weekly (H or W) extract in the 2nd argument" fi however, it always goes... (4 Replies)
Discussion started by: MIA651
4 Replies

2. Programming

Python passing filename through argument

Hello, A python beginner question on passing filename thru argument. My code is: #!/usr/bin/python import sys, getopt import os def main(argv): try: opts, args = getopt.getopt(sys.argv,"hi:o:ce", ) except getopt.GetoptError: usage() print("Usage: %s... (6 Replies)
Discussion started by: yifangt
6 Replies

3. Shell Programming and Scripting

Argument passing

How to pass the alphabet character as a argument in case and in if block? ex: c=$1 if a-z ]] then echo "alphabet" case $1 in a-z) echo "the value is a alphabet" edit by bakunin: please use CODE-tags. We REALLY mean it. (9 Replies)
Discussion started by: Roozo
9 Replies

4. Shell Programming and Scripting

Help with passing argument

Hi, I have a script that is scheduled with cron and runs every night. The cron part looks like this: 00 20 * * 0,1,2,3,4,5,6 /usr/local/bin/BACKUP TBTARM HOT DELETE My issue is with the 3rd parameter. Somewhere in the script, i want to tell the script to delete some files if the 3rd... (7 Replies)
Discussion started by: dollypee
7 Replies

5. Shell Programming and Scripting

passing an option as an argument!

Hi Folks I have got to the point where I can specify the arguments but how to pass an option is still mystery to me. Example: temp.csh a b c d set temp1 = $argv set temp2 = $argv set temp3 = $argv echo $temp1 a echo $temp2 b echo $temp3 c d I WANT: temp.csh a b c d -S 1 set temp1... (2 Replies)
Discussion started by: dixits
2 Replies

6. Shell Programming and Scripting

passing either of argument, not both in shell

Hi, I have a requirement to work on script, it should take either of arguments. wrote it as below. #!/bin/bash usage() { echo "$0: missing argument OR invalid option ! Usage : $0 -m|-r|-d } while getopts mrdvh opt; do case "$opt" in m) monitor_flag=monitor;;... (1 Reply)
Discussion started by: ramanaraoeee
1 Replies

7. Shell Programming and Scripting

Recursive argument passing

I'm writing a script that can be called on itself, and must be able to pass arguments down to itself properly. The regular usage of the script is as follows: myfun cmd1 'options1' cmd2 'options2' input If you're interested, the nuts and bolts of this function simply compare the outputs of... (2 Replies)
Discussion started by: Sunlis
2 Replies

8. Shell Programming and Scripting

passing Argument

Hi All, i have script like below.. echo "1) first option" echo "" echo "2) second option" echo "" echo "*) please enter the correct option" read select case $select in 1) echo "first option selected" ;; 2) echo "second option selected" ;; *) echo "please enter the correct... (4 Replies)
Discussion started by: Shahul
4 Replies

9. Shell Programming and Scripting

Problem with Argument Passing

Greetings, I am wrapping the monitoring commands like vmstat, sar, iostat and call via arguments I want ./unix_stats.sh -v vmstat -p <SEC> -d <Duration> to give vmstat values, and similarly iostat etc.,. Also if I give ./unix_stats.sh -v vmstat -i iostat -p <SEC> -d <Duration> should give... (4 Replies)
Discussion started by: A_Rod
4 Replies

10. Programming

Thread Argument Passing

#include <stdio.h> #include <pthread.h> #define NUM_THREADS 4 /* function to be executed by the new thread*/ void *PrintHello(void * threadid) { printf("\n %3d:Hello World!\n",threadid); pthread_exit(NULL); } int main(int argc, char * argv) { int *taskids; int... (2 Replies)
Discussion started by: narom
2 Replies
Login or Register to Ask a Question