Python Forum
Global Variables. Migrate code from MatLab
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global Variables. Migrate code from MatLab
#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.
Reply


Messages In This Thread
RE: Global Variables. Migrate code from MatLab - by Felipe - Jan-11-2017, 09:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand global variables 357mag 5 1,242 May-12-2023, 04:16 PM
Last Post: deanhystad
  Global variables or local accessible caslor 4 1,134 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,241 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 1,018 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  migrate code from tkinter to pygame Frankduc 16 3,395 Jun-01-2022, 12:45 PM
Last Post: Frankduc
  Global variables not working hobbyist 9 4,891 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global vs. Local Variables Davy_Jones_XIV 4 2,737 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,419 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  Question regarding local and global variables donmerch 12 5,287 Apr-12-2020, 03:58 PM
Last Post: TomToad
  local/global variables in functions abccba 6 3,555 Apr-08-2020, 06:01 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020