I have a very complicated formula, which I've simplified for the purpose of the question:
Position = θ_Zenith * θ_Azimuth
The Position formula will change over time, and thus I'd like to include it at the top of my .py script.
Python rejects this being at the top as there are no known values for θ_Zenith & θ_Azimuth.
If I place temporary values above the formula, such as:
θ_Zenith = 1
θ_Azimuth = 1
I receive an error when they're updated on later line;
θ_Zenith = 2.5
and Print;
print(Position)
I am still only getting a result for 1*1 = 1.
How can I get Python to update the variables within 'Position' formula prior to printing?
I guess you were writing in a form of, like:
θ_Zenith = 1
θ_Azimuth = 1
Position = θ_Zenith * θ_Azimuth
and, after that, re-assign the θ_Zenith
with 2.5 and print the value like:
θ_Zenith = 2.5
print(Position)
If that's it, it's simply because of the re-assignment afterwards will not update the mathematical function you have already assigned with those two variables. If you print the id
of all those variables, it's easily to find that the variable Position
remains unchanged. You have to re-assign the Position
as well.
Once you have assigned variables with float
or int
, which are separate and immutable objects, variable names are references only and id
s are the only fact. If you print the id
of each int
and float
you've assigned (i.e. id(1), id(2.5)
), you will suprising find that it is the same as variable one.
θ_Zenith = 1
θ_Azimuth = 1
print(id(θ_Zenith), id(θ_Azimuth), id(1))
# Out: 4498850032 4498850032 4498850032
Position = θ_Zenith * θ_Azimuth
print(id(Position), id(1)==id(Position))
# Out: 4498850032 True
θ_Zenith = 2.5
print(Position)
# Out: 1
print(id(θ_Zenith), id(2.5))
# Out: 4499936368 4499936368
print(id(θ_Azimuth), id(Position), id(1))
# Out: 4498850032 4498850032 4498850032
# You may find that the id of 'θ_Zenith' is changed, but not the one of 'Position'
Position = θ_Zenith * θ_Azimuth # this will update Position after re-assignment
print(Position)
# Out: 2.5
However, this is too tedious as you have to update the value of variable Position
every single time; write a function instead. Something like user @RainFlyWave suggested:
def position(variable_a: float, variable_b: float) -> float:
return variable_a * variable_b