Passing a string argument by reference

A

Andrew Chalk

I am a raw beginner to Python. I just read in "Learning Python" that
assigning to a string argument inside a function does not change the string
in the caller. I want an assignment in the function to alter the passed
string in the caller. Is there any way to do this?

For example

def SafeAdd(self, Variable, Value):
if self.form.has_key( Value ):
Variable = self.form[Value].value

Called with:

self.SafeAdd(self.txtCIF, 'txtCIF')

self.CIF is not changed on return from the function. How do I modify this so
that it is?

Many thanks
 
T

Terry Reedy

Andrew Chalk said:
I am a raw beginner to Python. I just read in "Learning Python" that
assigning to a string argument inside a function does not change the string
in the caller. I want an assignment in the function to alter the passed
string in the caller. Is there any way to do this?

As asked, no. Strings are immutable. Period.

However, with the proper infor passed in, you may be able to rebind a
name or other target to a new (string) object.
For example

def SafeAdd(self, Variable, Value):
if self.form.has_key( Value ):
Variable = self.form[Value].value

Called with:

self.SafeAdd(self.txtCIF, 'txtCIF')

------------
I think you want settattr here.

Help on built-in function setattr:

setattr(...)
setattr(object, name, value)

Set a named attribute on an object; setattr(x, 'y', v) is
equivalent to
``x.y = v''.
----------
Example:1
-------------
Perhaps you want something like 'setattr(self, Value,
self.form[Value].value)'. The param Variable is useless. Arg
self.txtCIF, for instance, is the object currently bound to the name
'txtCIF' and has no info about what name it was bound to. The string
arg such as 'txtCIF' appears to be all you need in the case.

Terry J. Reedy
 
I

Irmen de Jong

Andrew said:
I am a raw beginner to Python. I just read in "Learning Python" that
assigning to a string argument inside a function does not change the string
in the caller. I want an assignment in the function to alter the passed
string in the caller. Is there any way to do this?

No. Strings are immutable. However, there is a better solution to do what
you want: just return the new value as a result of the function, and
let the caller process this further.

Example:

def addBarToString(variable):
return variable+"bar"

called with:

variable="foo"
variable=addBarToString(variable)
print variable

results in >>foobar<< to be printed.

For example

def SafeAdd(self, Variable, Value):
if self.form.has_key( Value ):
Variable = self.form[Value].value

Called with:

self.SafeAdd(self.txtCIF, 'txtCIF')

self.CIF is not changed on return from the function. How do I modify this so
that it is?

I understand that you want to assign self.txtCIF (not self.CIF as you
wrote) the value of self.form['txtCIF'], but only if that value
occurs in self.form?

There is a much easier way to do this. I suspect that self.form is
a dictionary (or a dict-like object).

Just use:

self.txtCIF = self.form.get('txtCIF', self.txtCIF)

The second argument to the get method is the default value that is
returned if the required key is not present in the dict.
In this case, it returns the 'old' value of self.txtCIF, so in effect,
self.txtCIF will be unchanged if 'txtCIF' does not occur in self.form.
Also see http://www.python.org/doc/current/lib/typesmapping.html

Hope this helps!
--Irmen de Jong
 
A

Andrew Chalk

Thanks. You guessed correctly about what I am trying to do although I think
the syntax is:

self.txtCIF = self.form.getvalue('txtCIF', self.txtCIF)

Regards

Irmen de Jong said:
Andrew said:
I am a raw beginner to Python. I just read in "Learning Python" that
assigning to a string argument inside a function does not change the string
in the caller. I want an assignment in the function to alter the passed
string in the caller. Is there any way to do this?

No. Strings are immutable. However, there is a better solution to do what
you want: just return the new value as a result of the function, and
let the caller process this further.

Example:

def addBarToString(variable):
return variable+"bar"

called with:

variable="foo"
variable=addBarToString(variable)
print variable

results in >>foobar<< to be printed.

For example

def SafeAdd(self, Variable, Value):
if self.form.has_key( Value ):
Variable = self.form[Value].value

Called with:

self.SafeAdd(self.txtCIF, 'txtCIF')

self.CIF is not changed on return from the function. How do I modify this so
that it is?

I understand that you want to assign self.txtCIF (not self.CIF as you
wrote) the value of self.form['txtCIF'], but only if that value
occurs in self.form?

There is a much easier way to do this. I suspect that self.form is
a dictionary (or a dict-like object).

Just use:

self.txtCIF = self.form.get('txtCIF', self.txtCIF)

The second argument to the get method is the default value that is
returned if the required key is not present in the dict.
In this case, it returns the 'old' value of self.txtCIF, so in effect,
self.txtCIF will be unchanged if 'txtCIF' does not occur in self.form.
Also see http://www.python.org/doc/current/lib/typesmapping.html

Hope this helps!
--Irmen de Jong
 
I

Irmen de Jong

Andrew said:
Thanks. You guessed correctly about what I am trying to do although I think
the syntax is:

self.txtCIF = self.form.getvalue('txtCIF', self.txtCIF)

Please don't top-post.

Anyway it looks like form is actually a FieldStorage object from the cgi module,
am I right? It would have helped (a bit) if you pointed that out earlier.
But, never mind, it works now! Glad to be able to help you out.

--Irmen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top