473,378 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Need advice on python importing

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/...l-objects.html

So, partials won't work.

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

All thoughts are welcome.
Oct 17 '08 #1
3 1061
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/...l-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/

Oct 17 '08 #2
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/...l-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!
Oct 17 '08 #3
On Oct 17, 12:19 pm, Matthew Wilson <m...@tplus1.comwrote:
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/...l-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
Oct 17 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jeff Wagner | last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting...
12
by: JD | last post by:
This is another Python problem, I think might be unrelated to the earlier bug I found, and eventually figured out how to report it to Sourceforge. This is related to a question I have about...
9
by: Rick Muller | last post by:
I have a problem that I would like to get some advice on from other Pythonistas. I currently manage a (soon to be) open source project for displaying molecular graphics for a variety of different...
1
by: Robin Becker | last post by:
I'm testing ReportLab against Python-2.5beta1 and am getting some kind of problem as below ======================================================= C:\Python24\reportlab\test>\python25\python...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 342 open (-38) / 3712 closed (+54) / 4054 total (+16) Bugs : 951 open (-14) / 6588 closed (+33) / 7539 total (+19) RFE : 257 open...
0
by: Marco Bizzarri | last post by:
Hi all. I read the PEP8 and the "importing Python Modules" article. However, I'm still a little confused on what should the general rules for importing modules. I'm showing what I used in my...
0
by: Marco Bizzarri | last post by:
On Sat, Aug 30, 2008 at 4:53 PM, Eric Wertman <ewertman@gmail.comwrote: Thanks Eric; your 02 cents are worthy for me ;) Regards Marco --
7
by: SM | last post by:
Hello, I have a index.php template (2 columns). The right columns contains a bunch of links (interviews, poems, etc...) The left columns contains the actual article. So if I click on a link on...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.