Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global Variables. Migrate code from MatLab
#1
Hi guys,

During my undergraduation I wrote a program in MatLab. This program have functions separated in different files that are called in the main file. I want to pass the code to Python 3.5.2.

My program read a huge data base recorded with sensors installed on airplanes. I load this data base in the main program and every column is stored in vectors that are global variables. That way the functions can access the values without necessity to load the data base again.

The problem is that every explanation that I read, showed which I need to change the value of a global variable when I call it. For example:

def func1():
    global myGlobal
    myGlobal = 42
But I want to do something like that:

def func1():
    global myGlobal
    local = myGlobal
    # do any math which I need in this function with the "local" variable
I appreciate all help with this question.
Quote
#2
Do not use global Evil
You give argument to function and return value out.
metulburr and Mekire like this post
Quote
#3
I don't quite understand the question. Can you show the problem(s) that arise from not being able to do what you want to?
Quote
#4
(Jan-11-2017, 06:51 PM)snippsat Wrote: Do not use global Evil
You give argument to function and return value out.

Well that's a way, but some functions has a lots of arguments.
Using global I can simplify my code and avoid atributions errors.

(Jan-11-2017, 07:40 PM)micseydel Wrote: I don't quite understand the question. Can you show the problem(s) that arise from not being able to do what you want to?

Ok, I wrote this simple code to illustrate the problem.

Here's my main file:


#!/usr/bin/env python3

import numpy as np
from function import fwd

global I 

I = 90

result = fwd()
print(result)

My function "fwd" are in another file. This is the function code:


#!/usr/bin/env python3

def fwd():
    global I
    J = 10
    K = I + J
    return [K]

When I try to run I receive this error:

Error:
" NameError: name 'I' is not defined "
Basically, I want my program to call different functions, that can use the globals to do the math which I need to calculate different physics properties.

Thanks for the help.
Quote
#5
#!/usr/bin/env python3
import numpy as np
from function import fwd

global I # Make no sense since I is in global namespace

I = 90 # Is global

result = fwd()
print(result)
Here how you do it.
#bar.py
import numpy as np
from foo import fwd

I = 90
result = fwd(I)
print(result)
#foo.py
def fwd(I):
    J = 10
    K = I + J
    return [K]

if __name__ == '__main__':
    # Test this will not run on import
    I = 90
    print(fwd(I))
Running bar.py:
λ python bar.py
[100]
Quote:Using global I can simplify my code and avoid atributions errors.
No,that not an excuse at all for using stupid global.
global destroy the whole point of functions,
which is keeping code local to function and not mix in with stuff in global namespace.
If it to difficult with function,using class is the next step.
Quote
#6
@Felipe: you don't need the global keyword for that example, and I'm no expert on that keyword but I think it just makes it global to the file, no farther. You can simply import the variable into the other file (again, no need for the global keyword).
Quote
#7
(Jan-11-2017, 10:45 PM)snippsat Wrote: Using global I can simplify my code and avoid atributions errors.
No,that not an excuse at all for using stupid global.
global destroy the whole point of functions,
which is keeping code local to function and not mix in with stuff in global namespace.
If it to difficult with function,using class is the next step.
OK, thanks for the help.

Globals are really common in MatLab and I wondering if in Python exist similar way to work with that.
As I can see, your suggestion is to work with functions like in C. It's a little bit more complicate to adapt my program, but I want to use Python, so I will try.

(Jan-11-2017, 10:53 PM)micseydel Wrote: @Felipe: you don't need the global keyword for that example, and I'm no expert on that keyword but I think it just makes it global to the file, no farther. You can simply import the variable into the other file (again, no need for the global keyword).

Oh, really, only for the file?

You can give me a short example from what you mean with "import the variable into the other file"? It's different from use arguments into functions?

Thanks.
Quote
#8
(Jan-11-2017, 11:07 PM)Felipe Wrote: You can give me a short example from what you mean with "import the variable into the other file"? It's different from use arguments into functions?
They are different, but looking more closely, you can't do it because the files would have to import from each other.

Is I a variable or a constant? Things are hairy if it's a variable. If it's just a constant, you can keep it in a separate file for constants if it's needed by multiple things (or needs to be easy to change) and if it's just needed in one place, that's probably where it should live unless there's a good reason to put it somewhere else.
Quote
#9
(Jan-12-2017, 04:35 PM)micseydel Wrote:
(Jan-11-2017, 11:07 PM)Felipe Wrote: You can give me a short example from what you mean with "import the variable into the other file"? It's different from use arguments into functions?
They are different, but looking more closely, you can't do it because the files would have to import from each other.

Is I a variable or a constant? Things are hairy if it's a variable. If it's just a constant, you can keep it in a separate file for constants if it's needed by multiple things (or needs to be easy to change) and if it's just needed in one place, that's probably where it should live unless there's a good reason to put it somewhere else.

Hi,

The values are constants. Some like I are constants with integer values, others are vectors with N positions and float values.
For example, I = 90 is a constant that means the inclination of the magnetic field in degrees, X = 1, 1.2, 1.4, 1.6, 1.8... is a vector with positions of measure in kilometers.

If I can write something like a function or structure, which I can call in the main program to access the values and do the math that I need, it's fine.

The problem of use arguments is because some functions call another functions. It obligate me to write functions that have many arguments. I'm concerning about the efficiency too, because my program invert a problem with many dimension using PSO. So the computational cost can be a problem.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to use .m matlab file in python ? sameer 5 95 May-10-2018, 09:39 AM
Last Post: wavic
  How to print a global dictionary class properly 3dimensions 2 137 Apr-18-2018, 05:45 PM
Last Post: nilamo
  Global Variable jarrod0987 4 155 Apr-06-2018, 03:09 AM
Last Post: jarrod0987
  Trouble with global variables maymac789 1 93 Mar-29-2018, 03:15 AM
Last Post: Mekire
  Convert indexing For Loop from MATLAB (uses numpy and pandas) bentaz 3 131 Mar-20-2018, 08:29 PM
Last Post: bentaz
  Saving data into .mat (Matlab Fiile) Tulo_Stone 0 88 Feb-28-2018, 12:57 AM
Last Post: Tulo_Stone
  global name 'completion_name_regex' is not defined tjnichols 10 336 Feb-22-2018, 06:55 PM
Last Post: tjnichols
  Is This Code Ok? How Can I Avoid Using Global Variables? digitalmatic7 9 266 Feb-15-2018, 09:32 AM
Last Post: DeaD_EyE
  Function Attribute / Global Variable Confusion digitalmatic7 1 130 Feb-13-2018, 09:54 AM
Last Post: DeaD_EyE
  Global vs local variable mquesnel 11 533 Jan-13-2018, 06:56 PM
Last Post: mquesnel

Forum Jump:


Users browsing this thread: 1 Guest(s)