Confused yet again: Very Newbie Question

Matt Nordhoff mnordhoff at mattnordhoff.com
Mon Jul 7 09:07:15 EDT 2008


mcl wrote:
> On 7 Jul, 13:09, Jeff <jeffo... at gmail.com> wrote:
>> 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.

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.

>>> def f():
...     return 1, 2
>>> one, two = f()
>>> one
1
>>> two
2

> Two:
> I am sorry but I do not understand the significance defining a Class
> as:
>             >>> class MyClass(object):
>   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
-- 



More information about the Python-list mailing list