This should be a simple question...

N

Neal Becker

Maybe I'm missing something obvious here

def A (...):
#set a bunch of variables
x = 1
b = 2
...

Do something with them

def B (...):
#set the same bunch of variables
x = 1
b = 2
...

Do something with them

I want to apply DRY, and extract out the common setting of these variables
into the local scope of the functions A and B. How to do this? (Other than
just setting them in the module scope)
 
D

Diez B. Roggisch

Neal said:
Maybe I'm missing something obvious here

def A (...):
#set a bunch of variables
x = 1
b = 2
...

Do something with them

def B (...):
#set the same bunch of variables
x = 1
b = 2
...

Do something with them

I want to apply DRY, and extract out the common setting of these variables
into the local scope of the functions A and B. How to do this? (Other than
just setting them in the module scope)

Using a class?

Diez
 
B

Bruno Desthuilliers

Neal Becker a écrit :
Maybe I'm missing something obvious here

def A (...):
#set a bunch of variables
x = 1
b = 2
...

Do something with them

def B (...):
#set the same bunch of variables
x = 1
b = 2
...

Do something with them

I want to apply DRY, and extract out the common setting of these variables
into the local scope of the functions A and B. How to do this? (Other than
just setting them in the module scope)

If your "variables" are literal constants common to the whole module,
and are *not* modified within the functions, the obvious answer is to
define them as module level (pseudo) symbolic constants.

Else, please provide more background.
 
N

Neal Becker

Bruno said:
Neal Becker a écrit :

If your "variables" are literal constants common to the whole module,
and are *not* modified within the functions, the obvious answer is to
define them as module level (pseudo) symbolic constants.

Else, please provide more background.

--

They are just a bunch of variables, but I don't want to put them into module
scope because there may be other functions C and D with different settings.

Of course I could collect them into an object and refer to them as o.x.
Requires more typing though.

What if I had:

my_obj = common_variables()
That set all these attributes, but then with function A I inject them into
A's scope (shouldn't be too hard to do, I think)?
 

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
474,294
Messages
2,571,511
Members
48,213
Latest member
DonnellTol

Latest Threads

Top