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
  • 1,798
  • 1
  • 21
  • 30

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
  • 227,678
  • 33
  • 352
  • 470
5

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

Dharmesh Fumakiya
  • 2,070
  • 1
  • 10
  • 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
  • 507
  • 12
  • 17