i've begun use python scripting language, i'm having difficulty understanding how should call objects file. because i'm not familiar on using attributes , methods.
for example, created simple quadratic formula script.
qf.py
#script solves equation of form a*x^2 + b*x + c = 0 import math def quadratic_formula(a,b,c): sol1 = (-b - math.sqrt(b**2 - 4*a*c))/(2*a) sol2 = (-b + math.sqrt(b**2 - 4*a*c))/(2*a) return sol1, sol2
so accessing script in python shell or file simple. can script output set if import function , call on it.
>>> import qf >>> qf.quadratic_formula(1,0,-4) (-2.0, 2.0)
but cannot access variables imported function, e.g. first member of returned set.
>>> print qf.sol1 traceback (most recent call last): file "<stdin>", line 1, in <module> attributeerror: 'module' object has no attribute 'sol1'
the same happens if merge namespaces imported file
>>> qf import * >>> quadratic_formula(1,0,-4) (-2.0, 2.0) >>> print sol1 traceback (most recent call last): file "<stdin>", line 1, in <module> nameerror: name 'sol1' not defined
is there better way call on these variables imported file? think fact sol1 & sol2 dependent upon given parameters (a,b,c) makes more difficult call them.
i think because sol1
, sol2
local variables defined in function. can
import qf sol1,sol2 = qf.quadratic_formula(1,0,-4) # sol1 = -2.0 # sol2 = 2.0
but sol1
, sol2
not same variables in qf.py
.
Comments
Post a Comment