473,804 Members | 3,894 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.parti al to bind on these parameters like this:

d1 = functools.parti al(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 1080
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.parti al to bind on these parameters like this:

d1 = functools.parti al(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
interdepende nt 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.parti al to bind on these parameters like this:

d1 = functools.parti al(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.co mwrote:
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.parti al to bind on these parameters like this:

d1 = functools.parti al(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
3110
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 that Numerology program I wrote years ago in VB. There are times I am totally stuck (for instance, I just had an idea to put the numerical values of the alphabet and months of the year in a dictionary located in a function. Then, I can import the...
12
2489
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 Python hanging up either from a shell, or by importing a module. This I would not consider a bug, but more on my inexperience with Python. If my code causes Python to just hang up, and not allowing me to break out with control-C or...
9
3197
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 programs. To do this I need to draw pictures of the molecules, typically using OpenGL, and do some basic control, using some widget set. The code currently uses GTK, and runs on Macintosh OS X under Fink, and Linux. I would like to support the...
1
1288
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 runAll.py ..C:\Python24\reportlab\test\test_docstrings.py:54: ImportWarning: Not importing directory 'C:\python25\reportlab\tools\utils': missing __init__.py module = __import__(mName) C:\Python24\reportlab\test\test_docstrings.py:54: ImportWarning:...
0
11477
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 (-15) / 266 closed (+13) / 523 total ( -2) New / Reopened Patches ______________________
0
689
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 current project, and will accept your advices on how I should change them. The style is consistently the following:
0
730
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
2172
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 the right menu, the article shows on the left column. The links have an url that look like this: http://www.myweb/library/?doc=194uf7s39
0
9710
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10593
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9163
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3000
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.