Confused yet again: Very Newbie Question

M

mcl

Why can I not the change the value of a variable in another class,
when I have passed it via a parameter list.

I am sure I am being stupid, but I thought passed objects were Read/
Write

eg
------------------------------------------------------------
#!/usr/bin/python

class one(): #my Global Vars
fred = 'fred'

class three():
def createJoe(self, myName):
c1 = one()
myName = 'Joe' #********************* Question why does this
not change variable fred in 'class one'
print 'Three(Local): ' + myName + ' Three(Global): ' +
c1.fred

def main():
c1 = one()
c3 =three()
c3.createJoe(c1.fred)


if __name__ == '__main__' : main()

Results:
Three(Local): Joe Three(Global): fred

'fred' in 'class one' does not get changed to 'joe' in 'class three'
'createJoe', even though I have passed 'class one' 'fred' to
'createJoe'

I hope this makes sense.

I did not think you had to make the distinction between 'byvar' and
'byref' as in Basic.

Thanks

Richard
 
M

Matt Nordhoff

mcl said:
Why can I not the change the value of a variable in another class,
when I have passed it via a parameter list.

I am sure I am being stupid, but I thought passed objects were Read/
Write

In Python, there are names which are bound to objects. Doing "foo = bar"
and then "foo = spam" (re)binds the name "foo" to the same object as
"spam" is bound to. This doesn't have any effect on any other names that
were bound to the same object as "bar".
eg
------------------------------------------------------------
#!/usr/bin/python

class one(): #my Global Vars
fred = 'fred'

class three():
def createJoe(self, myName):

Here, the local name "myName" is bound to the same string object as
one.fred.
c1 = one()
myName = 'Joe' #********************* Question why does this
not change variable fred in 'class one'

Here, you rebind the local name "myName" to the string object 'Joe'.
This doesn't change what one.fred is bound to.
print 'Three(Local): ' + myName + ' Three(Global): ' +
c1.fred

def main():
c1 = one()
c3 =three()
c3.createJoe(c1.fred)


if __name__ == '__main__' : main()

Results:
Three(Local): Joe Three(Global): fred

'fred' in 'class one' does not get changed to 'joe' in 'class three'
'createJoe', even though I have passed 'class one' 'fred' to
'createJoe'

I hope this makes sense.

I did not think you had to make the distinction between 'byvar' and
'byref' as in Basic.

Thanks

Richard

There are a couple good articles about this online. The one below is
lengthy and has ASCII art, but I don't remember if I liked it.

<http://starship.python.net/crew/mwh/hacks/objectthink.html>

BTW, in this example, there doesn't seem to be any need for you to be
using classes. Also, you should always use new-style classes unless you
have a specific reason not to
--
 
J

Jeff

When you call c3.createJoe(c1.fred), you are passing a copy of the
value stored in c1.fred to your function. Python passes function
parameters by value. The function will not destructively modify its
arguments; you must expliticly state your intention to modify an
object:

class one():
fred = 'fred'

class three():
def createJoe(self, myName):
return "Joe"

def main():
c1 = one()
c3 = three()
c1.fred = c3.createJoe() # Modify c1's attribute, fred, with the
return value from c3.createJoe
 
M

mcl

When you call c3.createJoe(c1.fred), you are passing a copy of the
value stored in c1.fred to your function.  Python passes function
parameters by value.  The function will not destructively modify its
arguments; you must expliticly state your intention to modify an
object:

class one():
    fred = 'fred'

class three():
    def createJoe(self, myName):
        return "Joe"

def main():
    c1 = one()
    c3 = three()
    c1.fred = c3.createJoe() # Modify c1's attribute, fred, with the
return value from c3.createJoe

Thank you very much for your helpful replies.

Two questions:
One:
My use of classes is because I want two classes one for global
variables and one for global functions.
A function may set multiple global variable values, so what is the
best way to do this as 'return' only appears to be able to set one
value.
Two:
I am sorry but I do not understand the significance defining a Class
as: what is object ?

I am using python with Google App Engine, and I only have Alex
Martelli's book up to Python 2.2, if they has any relevance ?

Thanks again for your help. I now understand what my mistakes were and
why I was not getting the results I had expected.

Richard
 
M

Matt Nordhoff

mcl said:
Thank you very much for your helpful replies.

Two questions:
One:
My use of classes is because I want two classes one for global
variables and one for global functions.

That's odd. Usually, you shouldn't be using any globals at all. Callers
should use their own local variables, or instances or whatever.
A function may set multiple global variable values, so what is the
best way to do this as 'return' only appears to be able to set one
value.

You can return a tuple, and then use tuple unpacking.
2

Two:
I am sorry but I do not understand the significance defining a Class
as:
what is object ?

I am using python with Google App Engine, and I only have Alex
Martelli's book up to Python 2.2, if they has any relevance ?

By inheriting from object, your class becomes a new-style class.
Otherwise, it's old-style. Honestly, I don't know all of the
differences, but new-style classes are better. For one thing, old-style
classes don't support properties or super().

They were introduced in Python 2.2. Your Python 2.2 book probably
centers around old-style classes.
Thanks again for your help. I now understand what my mistakes were and
why I was not getting the results I had expected.

Richard
--
 
P

Peter Pearson

My use of classes is because I want two classes one for global
variables and one for global functions.

One of the many lovely things about programming in the
Python style is that very few things need to be global.
When you make something global, you're in effect saying
that it has no particular connection to anything else.
If it has a connection to something else, then it should
probably be combined into an object with that something
else.
 
M

mcl

My use of classes is because I want two classes one for  global
variables and one for global functions.

        Which seems to violate the entire reason for using "classes" (and
sounds like something that Java's syntax forces upon one). Classes (and
instances of them) are typically defined to /combine/ the class/instance
specific data and the functions that operate upon that data into a
single object.

        If you just need a container for so-called "global" data, create a
module. Same for utility functions.

-=-=-=-=-=-             myGlobalData.py

something = 1
else = 3

-=-=-=-=-=-             myUtilityFuncs.py

def weird(a, b):
        return (a-b, b-a, a*b, a/b, float(a)/b)

-=-=-=-=-=-             main.py

import myGlobalData
import myUtilityFuncs

results = myUtilityFuncs.weird(myGlobalData.something,
                                                                myGlobalData.else)
print results
myGlobalData.something = results[2]
A function may set multiple global variable values, so what is the
best way to do this as 'return' only appears to be able to set one
value.

--
        Wulfraed        Dennis Lee Bieber               KD6MOG
        (e-mail address removed)             (e-mail address removed)
                HTTP://wlfraed.home.netcom.com/
        (Bestiaria Support Staff:               (e-mail address removed))
                HTTP://www.bestiaria.com/

Dennis,

Thank you for this reply. You are correct in what I was trying to do
and thank you for your very good example. I will now rewrite my code
changing classes to modules, which make more sense to me, even if it
is not proper Python.

I am much wiser about classes, but still a long way from fully
comprehending when and where to best use them.

Thank you all for your replies and links, as it solved my problems
enough, to get some code working.

Richard
 
T

Terry Reedy

Jeff said:
When you call c3.createJoe(c1.fred), you are passing a copy of the
value stored in c1.fred to your function. Python passes function
parameters by value.

These statements are both wrong. Function argument objects or objects
derived therefrom are bound to function parameter names. One could say
that Python calls by cross-namespace assignment. No copying is done,
anymore than with assignment statements.

It returns objects the same way if there is a target to receive them.
> The function will not destructively modify its arguments;

If the argument is mutable and the function calls a mutation method of
the object, it is mutated.

tjr
 
L

Lie

When you call c3.createJoe(c1.fred), you are passing a copy of the
value stored in c1.fred to your function.  Python passes function
parameters by value.

No, python doesn't pass variable either by value or by reference. The
behavior in python is a bit of mix between passing by value and by
reference, because python use different variable model than Basic (in
short, saying passing by reference or value in python doesn't make
sense). In python, variables are names referring to an object, in
Basic variables are the object itself. In python, when you pass
variable as function parameter, you pass the object to the function
and inside the function a new name is rebound to that object. These
two names are not related in any way except that they refers to the
same object.

In python an immutable object can't be changed after creation and
string is immutable, so when you reassign 'Joe' to myName, a string
object 'Joe' is created and assigned to myName, myName's original
string object ('fred') isn't affected and other names that refers to
the string object 'fred' (i.e. c1.fred, one.fred) isn't changed. If
you want to change all those values too, you'd have to change the
inner string object itself (which is impossible since string object is
immutable). If the object referred is mutable (e.g. list, dictionary)
that example would work.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top