0

Im not an expert, so be lenient.
Let's say I have two Python scripts:

script1.py:

def something(data):
    answer = data + 1
    return answer

script2.py:

import script1
data = 69
final = script1.something(data)
print final

If my understanding is correct, the above script2 should print 70, since script2 passed data variable to script1, which added 1 and returned a sum of 70.
My question is that is such a method of passing data among imported modules correct? If not what is the correct way?
And what happens if the name of the variable passed to script1 isn't data? What if it is something else like number.

Tonechas
  • 13,398
  • 16
  • 46
  • 80
Udit Dey
  • 183
  • 7
  • 3
    Why don't you just run your code and see if it works (it should). – martineau Jan 30 '16 at 15:27
  • @Udit: to keep things simpler, I'd recommend you to not think about modules unless you know how they work and why you need them. Just put everything in single file, at least for now. Focus on understanding functions before modules. – GingerPlusPlus Jan 30 '16 at 15:32
  • [4.6. Defining Functions](https://docs.python.org/2/tutorial/controlflow.html#defining-functions) – GingerPlusPlus Jan 30 '16 at 15:44

2 Answers2

0

I'm no expert at python either: but have you actually tried running model code in this format? As i can see nothing wrong with it. Also, it doesn't matter what name you give the argument. In this case as long as you give it a value it could be anything. For example it would also work to put:

import script1
number = 59
final = script1.something(number)
print final

and it should print 60. It would also work to give a value (i.e. 34) as the argument.

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
0

What you have looks correct. You could do this also:

import script1.something as someFunc
data = 69
final = someFunc(data)
print final

The name of the data variable doesn't matter because it's being passed into the function, which has a separate scope.

J. Titus
  • 9,535
  • 1
  • 32
  • 45