473,569 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing a string argument by reference

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_k ey( Value ):
Variable = self.form[Value].value

Called with:

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

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

Many thanks
Jul 18 '05 #1
4 13529

"Andrew Chalk" <ac****@XXXmagn acartasoftware. com> wrote in message
news:PP******** *************** @newssvr12.news .prodigy.com...
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_k ey( Value ):
Variable = self.form[Value].value

Called with:

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


------------
I think you want settattr here.
help(setattr)
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: class C: pass .... c=C()
setattr(c, 'a', 1)
c.a

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
Jul 18 '05 #2
Andrew Chalk wrote:
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=addBar ToString(variab le)
print variable

results in >>foobar<< to be printed.

For example

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

Called with:

self.SafeAdd(se lf.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

Jul 18 '05 #3
Thanks. You guessed correctly about what I am trying to do although I think
the syntax is:

self.txtCIF = self.form.getva lue('txtCIF', self.txtCIF)

Regards

"Irmen de Jong" <irmen@-NOSPAM-REMOVETHIS-xs4all.nl> wrote in message
news:3f******** *************** @news.xs4all.nl ...
Andrew Chalk wrote:
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=addBar ToString(variab le)
print variable

results in >>foobar<< to be printed.

For example

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

Called with:

self.SafeAdd(se lf.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

Jul 18 '05 #4
Andrew Chalk wrote:
Thanks. You guessed correctly about what I am trying to do although I think
the syntax is:

self.txtCIF = self.form.getva lue('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

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3951
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer http://alexvn.freeservers.com/s1/perfometer.html
3
14916
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
25
2909
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default construction and then.. // some other processing and/or changing 'retval' return retval; }
17
9357
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more commands on the same connection. I have an understanding that if SqlConnection is passed as "value" (unboxed), object B will create its own copy of...
2
10263
by: Witold Iwaniec via .NET 247 | last post by:
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net? Here is sample code from sample Asp.Net application. The sub loadValueByVal takes the argument by value so after returning to calling method, the object should be unchanged but it is not ...
3
2367
by: sd2004 | last post by:
I am still learning, could someone show/explain to me how to fix the error. I can see it is being wrong but do not know how to fix. could you also recommend a book that I can ref. to ? ////////////////// ERROR MESSAGE ///////////////////// bash-2.05b$ g++ test5c.cpp test5c.cpp: In function `int main()': test5c.cpp:36: error: invalid...
11
4116
by: Macca | last post by:
Hi, I'm writing an application that will pass a large amount of data between classes/functions. In C++ it was more efficient to send a pointer to the object, e.g structure rather than passing the actual structure itself. Is this true of C# also?
12
2669
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
7
5770
by: Johannes Bauer | last post by:
Hello Group, please consider the following code #include <vector> #include <iostream> #define USE_CONST #define USE_STRING
4
2111
by: puzzlecracker | last post by:
How can I pass a reference to a method as constant? I tried the following: Function(const Foo f) or Function(readonly Foo f) Also, How to declare local variable to be constant const Foo foo or readonlyFoo f?
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.