Need advice on python importing

Discussion in 'Python' started by Matthew Wilson, Oct 17, 2008.

  1. I started with a module with a bunch of classes that represent database
    tables. A lot of these classes have methods that use other classes
    inside, sort of like this:

    class C(object):
    @classmethod
    def c1(cls, a):
    return a

    class D(object):
    def d1(self, a):
    return a + C.c1(a)

    Notice how the d1 method on class D uses a classmethod c1 on C. C is in
    the same module, so it's globally available.

    I moved my classes C and D into different files, and then I noticed that
    D now needed to import C first. That worked fine until I wrote
    interdependent classes that couldn't import each other.

    So I passed in everything as parameters like this:

    def d1(self, C, a):

    That works fine, but now I've got some methods that have six
    parameters (or more) that are entirely just for this purpose. So, now
    instead of passing these in as parameters, I'm passing them into my
    initializer and binding them in to self.

    I wanted to use functools.partial to bind on these parameters like this:

    d1 = functools.partial(d1, C=C)

    But partial objects don't get the self parameter passed in, so using
    partial is is effectively similar to decorating methods with
    staticmethod. Here's a link to the documentation on partial that
    explains this:

    http://www.python.org/doc/2.5.2/lib/partial-objects.html

    So, partials won't work.

    I suspect that there's more elegant solutions for this.

    All thoughts are welcome.
     
    Matthew Wilson, Oct 17, 2008
    #1
    1. Advertising

  2. Matthew Wilson

    Steve Holden Guest

    Matthew Wilson wrote:
    > I started with a module with a bunch of classes that represent database
    > tables. A lot of these classes have methods that use other classes
    > inside, sort of like this:
    >
    > class C(object):
    > @classmethod
    > def c1(cls, a):
    > return a
    >
    > class D(object):
    > def d1(self, a):
    > return a + C.c1(a)
    >
    > Notice how the d1 method on class D uses a classmethod c1 on C. C is in
    > the same module, so it's globally available.
    >
    > I moved my classes C and D into different files, and then I noticed that
    > D now needed to import C first. That worked fine until I wrote
    > interdependent classes that couldn't import each other.
    >
    > So I passed in everything as parameters like this:
    >
    > def d1(self, C, a):
    >
    > That works fine, but now I've got some methods that have six
    > parameters (or more) that are entirely just for this purpose. So, now
    > instead of passing these in as parameters, I'm passing them into my
    > initializer and binding them in to self.
    >
    > I wanted to use functools.partial to bind on these parameters like this:
    >
    > d1 = functools.partial(d1, C=C)
    >
    > But partial objects don't get the self parameter passed in, so using
    > partial is is effectively similar to decorating methods with
    > staticmethod. Here's a link to the documentation on partial that
    > explains this:
    >
    > http://www.python.org/doc/2.5.2/lib/partial-objects.html
    >
    > So, partials won't work.
    >
    > I suspect that there's more elegant solutions for this.
    >
    > All thoughts are welcome.


    Explain why you are using classmethods instead of regular methods in the
    first case (I appreciate your actual code will be rather more complex
    than your examples).

    regards
    Steve

    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Oct 17, 2008
    #2
    1. Advertising

  3. On Fri 17 Oct 2008 04:52:47 PM EDT, Steve Holden wrote:
    > Matthew Wilson wrote:
    >> I started with a module with a bunch of classes that represent database
    >> tables. A lot of these classes have methods that use other classes
    >> inside, sort of like this:
    >>
    >> class C(object):
    >> @classmethod
    >> def c1(cls, a):
    >> return a
    >>
    >> class D(object):
    >> def d1(self, a):
    >> return a + C.c1(a)
    >>
    >> Notice how the d1 method on class D uses a classmethod c1 on C. C is in
    >> the same module, so it's globally available.
    >>
    >> I moved my classes C and D into different files, and then I noticed that
    >> D now needed to import C first. That worked fine until I wrote
    >> interdependent classes that couldn't import each other.
    >>
    >> So I passed in everything as parameters like this:
    >>
    >> def d1(self, C, a):
    >>
    >> That works fine, but now I've got some methods that have six
    >> parameters (or more) that are entirely just for this purpose. So, now
    >> instead of passing these in as parameters, I'm passing them into my
    >> initializer and binding them in to self.
    >>
    >> I wanted to use functools.partial to bind on these parameters like this:
    >>
    >> d1 = functools.partial(d1, C=C)
    >>
    >> But partial objects don't get the self parameter passed in, so using
    >> partial is is effectively similar to decorating methods with
    >> staticmethod. Here's a link to the documentation on partial that
    >> explains this:
    >>
    >> http://www.python.org/doc/2.5.2/lib/partial-objects.html
    >>
    >> So, partials won't work.
    >>
    >> I suspect that there's more elegant solutions for this.
    >>
    >> All thoughts are welcome.

    >
    > Explain why you are using classmethods instead of regular methods in the
    > first case (I appreciate your actual code will be rather more complex
    > than your examples).


    Hi Steve, I'm using SQLObject classes, and joining one table with
    another requires either the class or an instance for the class. So, I
    could pass in instances of all the classes, and I'd be at the same
    point. I just don't like seeing functions with lots and lots of
    parameters.

    Thanks for the feedback!
     
    Matthew Wilson, Oct 17, 2008
    #3
  4. On Oct 17, 12:19 pm, Matthew Wilson <> wrote:

    > I started with a module with a bunch of classes that represent database
    > tables. A lot of these classes have methods that use other classes
    > inside, sort of like this:
    >
    > class C(object):
    > @classmethod
    > def c1(cls, a):
    > return a
    >
    > class D(object):
    > def d1(self, a):
    > return a + C.c1(a)
    >
    > Notice how the d1 method on class D uses a classmethod c1 on C. C is in
    > the same module, so it's globally available.
    >
    > I moved my classes C and D into different files, and then I noticed that
    > D now needed to import C first. That worked fine until I wrote
    > interdependent classes that couldn't import each other.
    >
    > So I passed in everything as parameters like this:
    >
    > def d1(self, C, a):
    >
    > That works fine, but now I've got some methods that have six
    > parameters (or more) that are entirely just for this purpose. So, now
    > instead of passing these in as parameters, I'm passing them into my
    > initializer and binding them in to self.
    >
    > I wanted to use functools.partial to bind on these parameters like this:
    >
    > d1 = functools.partial(d1, C=C)
    >
    > But partial objects don't get the self parameter passed in, so using
    > partial is is effectively similar to decorating methods with
    > staticmethod. Here's a link to the documentation on partial that
    > explains this:
    >
    > http://www.python.org/doc/2.5.2/lib/partial-objects.html
    >
    > So, partials won't work.
    >
    > I suspect that there's more elegant solutions for this.
    >
    > All thoughts are welcome.


    Cluttering the method signatures like this is worse than the problem
    they're trying (unsuccessfully) to solve. You should try to resolve
    the cyclic dependencies first. If it's not possible, that's probably a
    sign that they should live in the same module after all.

    George
     
    George Sakkis, Oct 18, 2008
    #4
    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. Asun Friere
    Replies:
    1
    Views:
    520
    Paul Boddie
    Aug 27, 2003
  2. Peter Hansen
    Replies:
    23
    Views:
    902
    Anton Vredegoor
    Sep 5, 2003
  3. Gerrit Holl
    Replies:
    16
    Views:
    637
    Tom Plunket
    Aug 29, 2003
  4. Asun Friere
    Replies:
    0
    Views:
    489
    Asun Friere
    Aug 28, 2003
  5. plb
    Replies:
    2
    Views:
    364
Loading...

Share This Page