Passing a string argument by reference

Discussion in 'Python' started by Andrew Chalk, Aug 3, 2003.

  1. Andrew Chalk

    Andrew Chalk Guest

    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
    Andrew Chalk, Aug 3, 2003
    #1
    1. Advertising

  2. Andrew Chalk

    Terry Reedy Guest

    "Andrew Chalk" <> wrote in message
    news:pPdXa.1338$...
    > 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
    Terry Reedy, Aug 3, 2003
    #2
    1. Advertising

  3. 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
    Irmen de Jong, Aug 3, 2003
    #3
  4. Andrew Chalk

    Andrew Chalk Guest

    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" <> wrote in message
    news:3f2d728a$0$49117$4all.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, Aug 4, 2003
    #4
  5. 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
    Irmen de Jong, Aug 4, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anon Email
    Replies:
    3
    Views:
    345
    Anon Email
    Jan 18, 2004
  2. Michael G

    passing an argument by reference

    Michael G, May 3, 2004, in forum: C++
    Replies:
    16
    Views:
    448
    Rob Williscroft
    May 4, 2004
  3. Trans
    Replies:
    3
    Views:
    184
    Robert Klemme
    Jan 26, 2005
  4. ±è ÁØ¿µ
    Replies:
    7
    Views:
    238
    Einar Magnús Boson
    Nov 27, 2008
  5. User1014
    Replies:
    1
    Views:
    176
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page