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 4 13517
"Andrew Chalk" <ac****@XXXmagnacartasoftware.com> wrote in message
news:PP***********************@newssvr12.news.prod igy.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_key( Value ): Variable = self.form[Value].value
Called with:
self.SafeAdd(self.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
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=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
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" <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=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
Andrew Chalk wrote: 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alex Vinokur |
last post by:
Various forms of argument passing
=================================
C/C++ Performance Tests
=======================
Using C/C++ Program Perfometer...
|
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) {...
|
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...
|
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...
|
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...
|
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 ?...
|
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...
|
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);
|
by: Johannes Bauer |
last post by:
Hello Group,
please consider the following code
#include <vector>
#include <iostream>
#define USE_CONST
#define USE_STRING
|
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...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
| |