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

A

Andrew Berg

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.
 
S

Steven D'Aprano

Andrew said:
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).


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)
 
A

Andrew Berg

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)
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).
 
S

Steven D'Aprano

Andrew said:
I need to affect the instance created in the main script;

Then call the methods on the instance created in the main script. I'm still
failing to see your problem.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top