Is there any way to access attributes from an imported module?

Discussion in 'Python' started by Andrew Berg, Oct 2, 2011.

  1. Andrew Berg

    Andrew Berg Guest

    I'm not sure the subject's wording is the best, but I'll try to explain.
    I have a main script that imports several modules and I need to be able
    to call methods from a class instance inside that main script from a
    module. Currently, functions can be defined to access the methods, but
    such functions can only be called via commands sent to the main script
    (it's an IRC bot, and commands are sent via IRC). What I want to do is
    call those methods without sending commands (I want to send an IRC
    message from an except clause). I'm not too sure which bits are
    relevant, and it's probably a lot of code for a post, so I'll link:

    Main script (DelphiBot().load() is where commands are processed):
    https://github.com/sthrs/Beacon/blob/501ab1ffef83aa13613edb5985ad9a4570245a85/main.py

    The module I'm working on with examples of both functions that use
    commands and functions that don't:
    https://github.com/sthrs/Beacon/blob/501ab1ffef83aa13613edb5985ad9a4570245a85/modules/lastfm.py


    I have developer access to the repo, so modifying the main script is not
    an issue (I realize there is a very good possibility this will require
    changing the way the bot handles loading commands). A push in the right
    direction would be greatly appreciated, as I'm quite stumped.
     
    Andrew Berg, Oct 2, 2011
    #1
    1. Advertisements


  2. Have I missed something? Why can't you just import the module and call the
    methods like you would for any other module and class?

    import module
    instance = module.Some_Class()
    result = instance.method(some, arguments, may, be, needed)
     
    Steven D'Aprano, Oct 2, 2011
    #2
    1. Advertisements

  3. Andrew Berg

    Andrew Berg Guest

    I need to affect the instance created in the main script; creating a new
    instance would be pointless (and AFAICT would result in the interpreter
    hitting a recursion limit since the module is imported during creation
    of the instance).
     
    Andrew Berg, Oct 2, 2011
    #3
  4. Then call the methods on the instance created in the main script. I'm still
    failing to see your problem.
     
    Steven D'Aprano, Oct 2, 2011
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.