Python Forum
Passing writable arguments to functions.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing writable arguments to functions.
#1
Sorry for beating so fundamental a question into the ground, but as a FORTRAN programmer, I feel pain with python functions.

Consider the function and function call:

def func (k)
  k = 3
  return

j = 0
func(j)
Upon return from "func", j is 0. Ouch.
Is there any way to persuade python to write to function arguments?
Gribouillis write Jan-12-2024, 09:29 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
(Jan-12-2024, 09:19 PM)Assembler Wrote: Is there any way to persuade python to write to function arguments?
No, there is no way to do that. There is no « reference to a variable » in Python. Use
>>> def func(k):
...     return 3
... 
>>> j = 0
>>> j = func(j)
>>> j
3
It is only a matter of habit. Python code typically solves this problem by writing inside structured objects that are passed to the function. For example
>>> class Spam:
...     def __init__(self):
...         self.j = 0
... 
>>> def func(s):
...     s.j = 3
... 
>>> x = Spam()
>>> func(x)
>>> x.j
3
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Thanks!
My searches turned up all sorts of geekery discussing various parameter schemes, but I did not see "No, there is no way to do that." Or if I did, I didn't believe it.
And thanks for the pointer to BBCode. That got by me.
Reply
#4
(Jan-12-2024, 10:50 PM)Assembler Wrote: Thanks!
My searches turned up all sorts of geekery discussing various parameter schemes, but I did not see "No, there is no way to do that." Or if I did, I didn't believe it.
And thanks for the pointer to BBCode. That got by me.

Although you had your questions answered, I would really recommend you to read up on pass-by-value vs pass-by-reference arguments and what is the difference between passing a value by reference vs passing reference by value. Because you are having issues specifically because of this. It's not just "python's thing", but other languages have similar scheme and some others are even more confusing.
Reply
#5
Python doesn't pass arguments by value or reference as both depend upon a definition of "variable" that doesn't exist in Python. Everything in Python is an object. You can use a name to make it easier to reference these objects, but the relationship between Python names, essentially keys in a dictionary, and the values they reference is nothing like a variable in Fortran or C, which is a memory location where a value is stored.
Reply
#6
(Jan-13-2024, 03:55 AM)deanhystad Wrote: Python doesn't pass arguments by value or reference as both depend upon a definition of "variable" that doesn't exist in Python. Everything in Python is an object. You can use a name to make it easier to reference these objects, but the relationship between Python names, essentially keys in a dictionary, and the values they reference is nothing like a variable in Fortran or C, which is a memory location where a value is stored.

IMO this is a distinction without a difference. For one, the semantics of argument passing is a design choice of a language, not an implementation detail which depends on the data structure. Another thing, the "dictionary" with it's keys IS a variable in memory. Maybe you mean they are not stored in registers or independent cells like they would in C++, but there are also slots in Python, which do make them memory locations and not dictionary entries.

And well, the way python is passing arguments is officially called "pass by assignment", which has the same semantics as assignment in python. In practice it is basically a pass-by-value scheme, the same is in Java or default c# (it has exactly the same semantics). Python passes references to each object by value. The reason primitive types are not changeable, unlike lists and other objects, is that they are immutable and when you assign a value to them, you get a new memory location and not a change at the old one (or call it new dictionary value).
All of which would be discovered if OP actually studied the scheme of passing arguments to a function.
Reply
#7
There is an interesting video by Ned Batchelder about names and values in Python. It is him who calls Python's calling scheme "call by assignment". I understand his point of view but I wouldn't call it the same. In CPython, functions are just passed a pointer to the structured object that represents the value and I think it is clear enough: a pointer to the value is passed (but not a handle...). This is relatively basic stuff.
sgrey likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#8
I wouldn't call them pass by assignment either, but it's written in the documentation this way https://docs.python.org/3/faq/programmin...-reference
Quote:Remember that arguments are passed by assignment in Python.
Overall, I pretty much agree :)
Reply
#9
(Jan-15-2024, 05:14 PM)sgrey Wrote: but it's written in the documentation this way
I don't know when they added this to the documentation. Ned Batchelder may be very well be the inspirator of this terminology.
« We can solve any problem by introducing an extra level of indirection »
Reply
#10
Yea, I was surprised as well, because there isn't really such a way to pass arguments. They are basically passing references by value, but they are grouping this with mutability semantics and calling it all together as pass by assignment, which is not even fully correct, but I guess does highlight the concept in way.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 420 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  Passing string functions as arguments Clunk_Head 3 1,268 Jun-15-2022, 06:00 AM
Last Post: Gribouillis
  Why Pass Functions as arguments? muzikman 14 5,697 Jan-18-2021, 12:08 PM
Last Post: Serafim
  redirect url_for passing arguments with the url Leon79 1 1,658 Jul-09-2020, 05:20 PM
Last Post: Leon79
  passing-arguments-from-one-script-to-another jacklee26 7 3,263 Apr-21-2020, 03:55 PM
Last Post: deanhystad
  Python 2.7 passing variables from functions zetto33 1 1,794 Mar-19-2020, 07:27 PM
Last Post: Larz60+
  Still confused about how passing arguments works RedSkeleton007 3 2,981 Apr-25-2018, 11:01 AM
Last Post: snippsat
  Trouble passing arguments underoathed 2 2,944 Nov-13-2017, 04:20 PM
Last Post: underoathed
  AttributeError: attribute 'port' of 'liblo._ServerBase' objects is not writable emrebt39 2 3,449 Jun-17-2017, 08:04 PM
Last Post: emrebt39
  Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names) Adelton 2 3,885 Mar-02-2017, 10:23 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020