3

I have many functions that all share the same parameter. They will be inputting and outputting this parameter many times.

For example:

a = foo
a = fun(a)
a = bar(a)

def fun(a):
     ...
     return a

def bar(a):
     ...
     return a

What is more pro-grammatically correct, passing parameters through a function, or having it be globally accessible for all the functions to work with?

a = foo
fun()
bar()

def fun():
    global a
    ...

def bar():
    global a
    ...
tisaconundrum
  • 2,156
  • 2
  • 22
  • 37

3 Answers3

6

The more localised your variables, the better.

This is virtually an axiom for any programming language.

structs in C (and equivalents in other languages such as FORTRAN) grew up from this realisation, and object orientated programming followed shortly after.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
5

For re-usability of method, passing parameter is better way.

Dharmesh Fumakiya
  • 2,276
  • 2
  • 11
  • 17
2

I agree with the other answers but just for the sake of completion, as others have pointed out a class sounds like a good idea here. Consider the following.

class myClass(object):
    def __init__(self, foo):
        self.a = foo

    def fun(self):
        # do stuff to self.a

    def bar(self):
        # do something else to self.a 

c = myClass(foo)
c.fun()
c.bar()
bouletta
  • 525
  • 9
  • 19