splitting common functions into a sepperate module

J

jonkersbart

Dear,

I have wrote a script and want to group some functions of the script
in a separate modulo so that I can import the module in other scripts
and use the same functions there..

The problem is that the common functions need access to some global
variables defined in the script. Python uses different namespaces for
different modules so I can't access the variables of the script in the
module.

What is the best solution to solve this problem?

Thanks in advance,
Bart
 
G

Gabriel Genellina

I have wrote a script and want to group some functions of the script
in a separate modulo so that I can import the module in other scripts
and use the same functions there..

The problem is that the common functions need access to some global
variables defined in the script. Python uses different namespaces for
different modules so I can't access the variables of the script in the
module.

Are those *really* global variables? If yes, you could put them in yet
another module, maybe config.py, and always get/set them using:
import config
config.xxx = "abc"
foo(x, config.yyy)
(do *not* use: from config import xxx)

Or, you may consider placing the global variables on the same module as
those common functions (if that make some sense...)

But perhaps they are not really global variables, and you can use some
class to hold them, specially if you can logically attach some methods to
it.

Without further knowledge of the application, that's all I can say.
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
Dear,

I have wrote a script and want to group some functions of the script
in a separate modulo so that I can import the module in other scripts
and use the same functions there..

The problem is that the common functions need access to some global
variables defined in the script.

And now you find out why globals are bad...
Python uses different namespaces for
different modules so I can't access the variables of the script in the
module.

What is the best solution to solve this problem?

There's no one-size-fits-all answer. Usual solutions include:
- passing the needed values as args to the functions
- wrapping the functions as methods of a class, passing the whole state
as args to the class initializer.
 
J

jonkersbart

(e-mail address removed) a écrit :




And now you find out why globals are bad...



There's no one-size-fits-all answer. Usual solutions include:

I was hoping for a one-size-fits-all answer. But it seems to be that
it doesn't exists.
I already know these solutions, but was hoping for a better one.
- passing the needed values as args to the functions
There are to many arguments so this is not a preferred solutions
- wrapping the functions as methods of a class, passing the whole state
as args to the class initializer.
I already considerate this one, but I don't like it because it is not
correct in terms of the object-oriented concept.
Because there are no other solutions I have to bury my bad feelings
and use this solution.

Thanks,
Bart
 
M

Marc 'BlackJack' Rintsch

jonkersbart said:
I already considerate this one, but I don't like it because it is not
correct in terms of the object-oriented concept.

Why? I thought combining state/data and related functions into one object
is a key concept of OOP.

Ciao,
Marc 'BlackJack' Rintsch
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
On 8 mrt, 10:36, Bruno Desthuilliers <bruno. (snip)
I already considerate this one, but I don't like it because it is not
correct in terms of the object-oriented concept.

Oh yes ? Why so ? You have a bunch of behaviors dependent on a same
state, don't you ?

Because there are no other solutions I have to bury my bad feelings
and use this solution.

Better to leave "correctness of object-oriented concept" to purists and
zealots, and use the appropriate *pragmatic* solution IMHO. No reason to
have "bad feelings" here.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top