Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Function parameters and initialization issue
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Srlancelot39




PostPosted: Wed Jan 10, 2018 11:44 am   Post subject: Function parameters and initialization issue

I have a function from one file, "file1.py", being called from another file, "main.py".

Some initializations are being done from within main.py (outside of a while statement calling the function), and these variables are passed into the function as a parameter. The variable is checked by an if statement (hence the need for initialization) before being reassigned a new value.

The issue is that each time the function is run the variable has the value it was given in main.py, despite having previously been changed. Some debugging showed that the initialization was only being run once, so that is ruled out. I have tried making the variable name in main.py different from the one used in file1.py (it's not actually called this by the way lol), but the problem persists.

main.py
code:
previous = "init"

while 1:
     AFunction(File1Function(SomeOtherFunction(), previous), previous)


file1.py
code:
def File1Function(SomeOtherFunction(), prevval):
     val = SomeOtherFunction()
          if not(val == prevval == "a certain value"):
               print val
               prevval = val


The if statement is always executed due to prevval always being "init" when the statement is checked. Any idea what I'm doing wrong? I know I can probably solve it with global variables, but I'd like to avoid those...

Thanks!



EDIT: Solved this a while ago using global variables, but please feel free to post any alternative solutions!
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: