Fwd: Calling a method from invoking module

Baskaran Sankaran baskarans at gmail.com
Thu Oct 28 23:33:41 EDT 2010


Sorry for the confusion; fooz(), track() and barz() are all members of their
respective classes. I must have missed the self argument while creating the
synthetic example.

Yeah, I realize the mutual import is a bad idea. So, if I merge them into a
single module (but still retaining the two classes) will work right? I
guess, it will look like this after merging.

Thanks again
-b

* foo_bar.py *

class Foo:
    def fooz(self):

        print "Hello World"
        b = Bar()
        c = b.barz()
        ...

    def track(self, track_var):
        count += 1
        return sth2


class Bar:
    def barz(self):

        track_this = ...
        if Foo.track(track_this):
            pass
        else:
            ...
        return sth1


On Thu, Oct 28, 2010 at 7:12 PM, Dave Angel <davea at ieee.org> wrote:

> Your example is so vague it's hard to tell what the real requirements are.
>  Foo.track() won't work as coded, since it doesn't have a self argument.  If
> you really meant that, then make it a non-class function, and preferably
> define it in another module, included perhaps by both bar and foo.
>  Similarly, barz() cannot be called, since it wants zero arguments, and
> it'll always get at least one.
>
> If I were you, I'd put both classes into the same module until you get
> their relationship properly understood.  And if they're really so
> intertwined, consider leaving them in the same module.  This isn't java.
>
> In general, it's best not to have two modules importing each other.
>  Generally, you can extract the common things into a separate module that
> each imports.  Failing that, you can have one import the other, and pass it
> whatever object references it'll need to run.  For example, at the end of
> foo.py, you'd have something like
>  bar.Foo = Foo
>
> If you really need to mutually import two modules, the first problem you're
> likely to bump into is if either of them is your script.  In other words,
> you need to have a separate file that's your script, that imports both foo
> and bar.
>
> DaveA
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101028/1a6f6a07/attachment.html>


More information about the Python-list mailing list