Using math in loaded module in Python 3 - python

in my main python file I just load the math module and an own module (called funcs.py). Finally, I run a function from the just loaded module.
import math
from funcs import *
RetentionTime(1,2,3,4)
The funcs.py file looks like this:
def RetentionTime(a, b, c, d):
"calculation of retention time"
RT = (11.2 * a) / (b * c * math.degrees( math.atan( d / 100 ) ) )
return RT
This leads to the following Nameerror:
NameError: name 'math' is not defined
In the python shell I can use commands like math.atan(...) without a problem. What am I doing wrong?
Thanks.

# test.py
y = 5
def f(x):
print(x+y)
Here f will bind the object named y from the innermost scope, which in this case is the module scope of test.py. Had it been as follows,
y = 5
def g():
y = 10
def f(x):
print(x+y)
return f
Here the y bound in f will be object 10. In your case RetentionTime is compiled in its own module scope and has no access to the scope of the caller. Hence add import math to the same module as RetentionTime.

Related

Python - How do I return a variable defined from an imported python function?

I've recreated this error in a simpler file.
main.py
from funcA import *
test()
print(x)
funcA.py
def test():
global x
y = 2
return x
I receive "NameError: name 'x' is not defined", what am I missing?
from funcA import * creates a new variable named x in the global scope of main.py. Its initial value comes from funcA.x, but it is a distinct variable that isn't linked to funcA.x.
When you call test, you update the value of funcA.x, but main.x remains unchanged.
After you call test, you can see the change by looking at funcA.x directly.
from funcA import *
import funcA
test()
print(funcA.x)
You have to create a global variable outside the function.
The global keyword cannot be used like that.
There's must be x outside the function.
# funcA.py
x = "something"
def test():
global x
y = 2
return x

Replace variable of a function while calling it in python?

Sorry for my bad english.
Is there any way to replace variable of a method from outside?
Suppose i have two files app.py and try.py.
In app -
def call():
c=5
return c
In try -
from app import *
c=1000
d=call()
print(d)
When i run try, here i want the output to be 1000 not 6. Is there any way to do this ?
I don't know any way to change c dynamically. Python compiles c = 5 into a LOAD_CONST op code as seen in the disassembly. And changing that op code would require, um, .... I don't know.
>>> from dis import dis
>>> def call():
... c=5
... return c
...
>>> dis(call)
2 0 LOAD_CONST 1 (5)
2 STORE_FAST 0 (c)
3 4 LOAD_FAST 0 (c)
6 RETURN_VALUE
You can monkey patch, though. Write your own implemenation of call and assign it dynamically at the start of your program.
import app
# from app, a reimplementation of call
def my_app_call_impl(c=1000):
return c
app.call = my_app_call_impl
add c argument to the function and use it like that
app file
def call(c):
return c
The second file
from app import *
c = 1000
d = call(c)
print(d)
if you want to c have a default value then app file should look like this
def call(c = 5):
You can define parameters for the call function:
app.py:
def call(c): # one parameter, c
return c
try.py
from app import *
c = 1000
d = call(c) # function called with c as an argument
print(d) # 1000
You can also use 5 as a default:
app.py
def call(c = 5):
return c
try.py
from app import *
d = call()
print(d) # 5
Functions in Python
*in app.py : *
def call(c=5):
return c
*in try.py : *
from app import *
d=call(1000)
print(d)

Module functions with default arguments and namespaces?

I want to collect a few generally useful function into a module my_module. These functions must have default arguments that are variables in the workspace. When I move these functions out from the main code to the module, and then import them into the main code, then I get an error since these default arguments cannot be found.
y = 1;
def f(x, y=y):
sum = x+y
return sum
f(1)
And when f instead is imported from my_module we have
y = 1
from my_module import f
f(1)
How can I adjust this later code to work as the first one?
my_module.py:
y = 1
def f(x, y = None):
if y is None:
y = globals()['y']
sum = x+y
return sum
test.py
import my_module
my_module.y = 2
f = my_module.f
print(f(1))

Call a function of inside function in main file from module using python

import math as m
def f1():
a = 10
b = 20
c = a+b
print(c)
print(m.sqrt(4))
def num(a,b):
d = a*b
return d
How to call to num function in another pycharm module ?
You can't, the function num(a,b) is essentially a local variable inside f1(); it only exists when f1() is ran.
Just write num(a,b) not as a nested function and you can call it elsewhere (if you import it properly) and f1() still has access to it:
import math as m
def num(a,b):
d = a*b
return d
def f1():
a = 10
b = 20
c = a+b
d = num(a,b)

Pass value to imported script in Python [duplicate]

This question already has answers here:
Using global variables in a function
(25 answers)
Closed 8 months ago.
I'm using functions so that my program won't be a mess but I don't know how to make a local variable into global.
Here are two methods to achieve the same thing:
Using parameters and return (recommended)
def other_function(parameter):
return parameter + 5
def main_function():
x = 10
print(x)
x = other_function(x)
print(x)
When you run main_function, you'll get the following output
>>> 10
>>> 15
Using globals (never do this)
x = 0 # The initial value of x, with global scope
def other_function():
global x
x = x + 5
def main_function():
print(x) # Just printing - no need to declare global yet
global x # So we can change the global x
x = 10
print(x)
other_function()
print(x)
Now you will get:
>>> 0 # Initial global value
>>> 10 # Now we've set it to 10 in `main_function()`
>>> 15 # Now we've added 5 in `other_function()`
Simply declare your variable outside any function:
globalValue = 1
def f(x):
print(globalValue + x)
If you need to assign to the global from within the function, use the global statement:
def f(x):
global globalValue
print(globalValue + x)
globalValue += 1
If you need access to the internal states of a function, you're possibly better off using a class. You can make a class instance behave like a function by making it a callable, which is done by defining __call__:
class StatefulFunction( object ):
def __init__( self ):
self.public_value = 'foo'
def __call__( self ):
return self.public_value
>> f = StatefulFunction()
>> f()
`foo`
>> f.public_value = 'bar'
>> f()
`bar`
Using globals will also make your program a mess - I suggest you try very hard to avoid them. That said, "global" is a keyword in python, so you can designate a particular variable as a global, like so:
def foo():
global bar
bar = 32
I should mention that it is extremely rare for the 'global' keyword to be used, so I seriously suggest rethinking your design.
You could use module scope. Say you have a module called utils:
f_value = 'foo'
def f():
return f_value
f_value is a module attribute that can be modified by any other module that imports it. As modules are singletons, any change to utils from one module will be accessible to all other modules that have it imported:
>> import utils
>> utils.f()
'foo'
>> utils.f_value = 'bar'
>> utils.f()
'bar'
Note that you can import the function by name:
>> import utils
>> from utils import f
>> utils.f_value = 'bar'
>> f()
'bar'
But not the attribute:
>> from utils import f, f_value
>> f_value = 'bar'
>> f()
'foo'
This is because you're labeling the object referenced by the module attribute as f_value in the local scope, but then rebinding it to the string bar, while the function f is still referring to the module attribute.

Categories