Automatic import ?

Discussion in 'Python' started by C. B., Mar 26, 2010.

  1. C. B.

    C. B. Guest

    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
     
    C. B., Mar 26, 2010
    #1
    1. Advertising

  2. 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
     
    Steven D'Aprano, Mar 26, 2010
    #2
    1. Advertising

  3. C. B.

    Ovidiu Deac Guest

    > 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
     
    Ovidiu Deac, Mar 26, 2010
    #3
  4. 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
     
    Jean-Michel Pichavant, Mar 26, 2010
    #4
  5. C. B.

    C. B. Guest

    At first, thank you all for your answers.

    Some more details about what I'm coding and what I need...

    The classes AAA and BBB are just given as examples. In fact, BBB's
    constructor accepts several parameters, meaning it can be different
    for each creation of AAA. So it can't be simply skipped from the AAA's
    one, even if there can be a default value.

    Making AAA do the call to BBB is a solution, but, in that case, the
    end-user loses the reference to the used BBB instance.

    Concerning the reason why I wish to avoid importing, once again AAA
    and BBB were just examples. I plan to add more classes, so AAA will
    also need CCC, DDD, and so on to work.

    It takes time to write the relative importations, that's ok, but I
    think it could be more pleasant for the end-user to not have to write
    a huge list of "from mymodule import xxx" if it is possible to take
    advantage of automatic importations.

    Cheers,
     
    C. B., Mar 27, 2010
    #5
  6. On Fri, 26 Mar 2010 18:18:07 -0700, C. B. wrote:

    > It takes time to write the relative importations, that's ok, but I think
    > it could be more pleasant for the end-user to not have to write a huge
    > list of "from mymodule import xxx" if it is possible to take advantage
    > of automatic importations.


    You mean by black magic? Something like this?


    >>> GGG = "My really important value"
    >>> from mymodule import AAA
    >>> print AAA()

    "something"
    >>> print GGG

    <class 'mymodule.GGG'>
    >>> print "WTF happened to my important value???"

    'magic ate it'


    As a general rule, Python does not allow magic. One exception is:

    from mymodule import *


    however once you have been bitten by it, you will soon learn to avoid it
    as almost always a bad idea.



    --
    Steven
     
    Steven D'Aprano, Mar 27, 2010
    #6
  7. C. B.

    MRAB Guest

    C. B. wrote:
    > At first, thank you all for your answers.
    >
    > Some more details about what I'm coding and what I need...
    >
    > The classes AAA and BBB are just given as examples. In fact, BBB's
    > constructor accepts several parameters, meaning it can be different
    > for each creation of AAA. So it can't be simply skipped from the AAA's
    > one, even if there can be a default value.
    >
    > Making AAA do the call to BBB is a solution, but, in that case, the
    > end-user loses the reference to the used BBB instance.
    >
    > Concerning the reason why I wish to avoid importing, once again AAA
    > and BBB were just examples. I plan to add more classes, so AAA will
    > also need CCC, DDD, and so on to work.
    >
    > It takes time to write the relative importations, that's ok, but I
    > think it could be more pleasant for the end-user to not have to write
    > a huge list of "from mymodule import xxx" if it is possible to take
    > advantage of automatic importations.
    >

    Couldn't you just have:

    import mymodule as m

    and then use:

    m.AAA
    m.BBB

    and so on?

    It's not much longer and it avoids 'magic'.
     
    MRAB, Mar 27, 2010
    #7
  8. C. B. wrote:
    > [snip]
    > It takes time to write the relative importations, that's ok, but I
    > think it could be more pleasant for the end-user to not have to write
    > a huge list of "from mymodule import xxx" if it is possible to take
    > advantage of automatic importations.
    >
    > Cheers,
    >


    In that particular case, replace automatic by implicit, and you got the
    reason why it is not a good idea.
    Maybe in your case the C habits clashes to the python habits.

    Talking about python, if the user needs to know about BBB, then it has
    to import it, perdiod. If the user needs to know about many objects,
    then it has to import them all, explicitly.
    However it looks like all your objects belong to the same module,
    quoting MRAB:

    "
    Couldn't you just have:

    import mymodule as m

    and then use:

    m.AAA
    m.BBB
    "


    You don't like prefixing your object with the namespace they belong to ?
    Well, you should :eek:)

    JM


    PS : I would'n have renamed the module, but that falls into personal
    preferences.
     
    Jean-Michel Pichavant, Mar 27, 2010
    #8
  9. C. B.

    C. B. Guest


    > In that particular case, replace automatic by implicit, and you got the
    > reason why it is not a good idea.
    > Maybe in your case the C habits clashes to the python habits.
    >


    You're right !

    As a C developer, I appreciate to only include <string.h> to deal with
    strings, without wondering which other header provides size_t
    definition.

    > Talking about python, if the user needs to know about BBB, then it has
    > to import it, perdiod. If the user needs to know about many objects,
    > then it has to import them all, explicitly.


    Ok. If this is the Python way of coding, that's ok for me. I will stop
    now my quest for an automatic import and work like that.

    The most interesting solution I have found until now is using
    PyImport_AppendInittab() and PyEval_GetGlobals() functions. But this
    also brings lots of problems (mainly namespaces and unloading).
    Anyway, the time spent to look for a solution was a nice way to learn
    Python internals :)

    Cheers,
     
    C. B., Mar 27, 2010
    #9
  10. C. B.

    Aahz Guest

    In article <4bac361d$0$8840$>,
    Steven D'Aprano <> wrote:
    >On Thu, 25 Mar 2010 18:03:58 -0700, C. B. wrote:
    >>
    >> 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()):
    > ...


    That would frequently give wrong results unless BBB is explicitly
    designed to create immutable instances. I strongly suggest doing the
    usual mutable dance:

    def AAA(arg=None):
    if arg is None:
    arg = BBB()
    --
    Aahz () <*> http://www.pythoncraft.com/

    "It is easier to optimize correct code than to correct optimized code."
    --Bill Harlan
     
    Aahz, Apr 10, 2010
    #10
    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. Guest
    Replies:
    1
    Views:
    791
    Guest
    Jun 29, 2004
  2. Paul Opal
    Replies:
    12
    Views:
    1,003
    Paul Opal
    Oct 11, 2004
  3. Keith Jackson

    automatic from module import * expansion

    Keith Jackson, Mar 13, 2006, in forum: Python
    Replies:
    2
    Views:
    309
    astyonax
    Mar 13, 2006
  4. Connelly Barnes

    Automatic import PEP

    Connelly Barnes, Sep 23, 2006, in forum: Python
    Replies:
    11
    Views:
    539
    Georg Brandl
    Oct 6, 2006
  5. Massi

    Automatic import of submodules

    Massi, Nov 25, 2011, in forum: Python
    Replies:
    4
    Views:
    274
    Shambhu Rajak
    Nov 28, 2011
Loading...

Share This Page