From: C. B. on
Hi everyone,

I'm currently coding a C library which provides several modules and
objects.

Let's say that some of these objects are classes called AAA and BBB.
The constructor of AAA needs to get BBB as argument.

So I can run the following code :

from mymodule import AAA
from mymodule import BBB

a = AAA(BBB()))

But, as there is no case where AAA can be used without BBB, I would
like to avoid importing BBB in my Python scripts when I already import
AAA.

For now, I think that reproducing the behavior of the __init__.py file
could be a good way to do this, but how can I code that using only the
C API ?

Are there any other solutions ? Is this kind of importation a good
idea ?

Greetings,
Cyrille Bagard
From: Steven D'Aprano on
On Thu, 25 Mar 2010 18:03:58 -0700, C. B. wrote:

> Hi everyone,
>
> I'm currently coding a C library which provides several modules and
> objects.
>
> Let's say that some of these objects are classes called AAA and BBB. The
> constructor of AAA needs to get BBB as argument.
>
> So I can run the following code :
>
> from mymodule import AAA
> from mymodule import BBB
>
> a = AAA(BBB()))
>
> But, as there is no case where AAA can be used without BBB, I would like
> to avoid importing BBB in my Python scripts when I already import AAA.

Since AAA must take an argument of BBB, then give it a default:

# in mymodule
def AAA(arg=BBB()):
...

# in another module
from mymodule import AAA
a = AAA()


Or do this:

from mymodule import AAA, BBB
a = AAA(BBB())




> For now, I think that reproducing the behavior of the __init__.py file
> could be a good way to do this, but how can I code that using only the C
> API ?

What is the behaviour of the __init__.py file?



--
Steven
From: C. B. on

>
> What is the behaviour of the __init__.py file?
>

Not yet used, but I read this file is run by Python when a module of a
package is imported.

So you can insert default importations in it.


From: Ovidiu Deac on
> But, as there is no case where AAA can be used without BBB, I would
> like to avoid importing BBB in my Python scripts when I already import
> AAA.

Then why do you need to explicitely pass BBB() to AAA constructor? You
could leave AAA constructor with no parameters or with a parameter
with a default BBB() value
From: Jean-Michel Pichavant on
C. B. wrote:
> Hi everyone,
>
> I'm currently coding a C library which provides several modules and
> objects.
>
> Let's say that some of these objects are classes called AAA and BBB.
> The constructor of AAA needs to get BBB as argument.
>
> So I can run the following code :
>
> from mymodule import AAA
> from mymodule import BBB
>
> a = AAA(BBB()))
>
> But, as there is no case where AAA can be used without BBB, I would
> like to avoid importing BBB in my Python scripts when I already import
> AAA.
>
> For now, I think that reproducing the behavior of the __init__.py file
> could be a good way to do this, but how can I code that using only the
> C API ?
>
> Are there any other solutions ? Is this kind of importation a good
> idea ?
>
> Greetings,
> Cyrille Bagard
>
Make AAA do the call to BBB:

def AAA(*args, **kwargs):
param = BBB(*args, **kwargs)
# more code

from mymodule import AAA

AAA(1,2,3) # list of parameters you would have used with BBB.

Saying that, is there any strong reason (meaning design issue) why you don't want to import explicitly BBB ?
Because if the reason is 'It takes too much time', then it's a bad reason.

Cheers,

JM