[Tutor] Working with Python Objects

Dinesh B Vadhia dineshbvadhia at hotmail.com
Sun Mar 16 04:33:39 CET 2008


I've combined your code fragments and added a function call too, to determine how 'a' is passed between objects and classes:

def addNumbers(i, j):
    k = i + j
    return k

class A:
    def oneA(self):
        z = 2
        self.a = self.a * z

class B:
    def oneB(self):
        inA = A()                 # instance of class A
        y = 5
        b = y * inA.a
        c = addNumbers(y, b)

Is this correct?


class A:
    constantA = 9
    def OneOfA:
            <do something>
            a = <do something else>

class B:
    variableB = "quick brown fox"
    def OneOfB:
            <do something>
            b = <do something more>
            c = b * a        # the 'a' from def OneOfA in class A
> Question:
> 1) how do I access the 'a' from function (method) OneOfA in
> class A so that it can be used by functions (methods) in class B?

You don't and shouldn't try to. In this case because the attriute
only exists inside the method, it is local, so dies when the
method completes. So first of all you need to make it part
of the class A. We do that by tagging it as an attribute of
self, which should be the fitrst attribute of every method.

But one of the concepts of OOP is to think in terms of the
objects not the attributes inside them So your question
should probably be: How do I access objects of class A
inside methods of class B?

The answer is by passing an instance into the method as a
parameter. You can then manipulate the instance of A by
sending messages to it. In Python you can access the
instance values of an object by sending a message with
the same name as the attribute - in other OOP languages
you would need to provide an accessor method.

But it is very important conceptually that you try to get away
from thinking about accessing attributes of another object
inside methods. Access the objects. Metthods should only
be manipulating the attributes of their own class. To do
otherwise is to break the reusability of your classes.

So re writing your pseudo code:

class A:
    constantA = 9
    def OneOfA(self):   # add self as first parameter
            <do something>
            self.a = <do something else>   # use 'self' to tag 'a' as 
an attribute

class B:
    variableB = "quick brown fox"
    def OneOfB(self, anA):    # add self and the instance of A
            <do something>
            b = <do something more>
            c = b * anA.a        # the 'a' from the instance anA

This way OneOfB() only works with attributes local to it
or defined as instance variables or passed in as arguments.
Which is as it should be!

Real OOP purists don't like direct attribute access but
in Python its an accepted idiom and frankly there is little
value in writing an accessor method that simply returns
the value if you can access it directly. The thing you
really should try to avoid though is modifying the attributes
directly from another class. Normally you can write a
more meaningful method that will do that for you.

Alan Gauld
Author of the Learn to Program web site
Temorarily at:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080315/3946a7b0/attachment.htm 

More information about the Tutor mailing list