I would like to place a function at the end of a script which is used in the script. This, of course, does not work as the function is not known yet.
Can I therefore import this funtion in the same script?
If I do it like the following, I get the error " ImportError: cannot import name:'subfunction' "
from the_script_in_use import subfuction
a=subfunction(b)
def subfunction(value)
do something
return a
One way to do this in Python is writing:
def main():
b = 5
a = subfunction(b)
print a
def subfunction(value):
a = value + 10
return a
if __name__ == '__main__':
main()
This way you can write your code in the order you like, as long as you keep calling the function main at the end.
You may use the statement :
if __name__ == '__main__':
Which will be executed if you run this file as a script. In your case:
def main_function()
a=subfunction(b)
def subfunction(value)
do something
return a
if __name__ == '__main__':
main_function()
Note there is no need to import your function.
Doing so you can order your fonction the way you want.
you can try to use pass :
def func1():
pass
func1()
def func1():
print "yes"
But this does not actually help you run the function.
Related
So there's two scripts: script1.py and script2.py. script1.py has a variable x that stores the string read from a file during runtime. In script2.py, I import script1.py and then run script1.py using the following: script1.main(). I then run a particular function in script1.py that is responsible for reading the file in question.
You might wonder why I don't just run that particular function in the first place instead of running the whole script. That's because script1.py does a bunch of other things that script2.py needs every time it is run.
I want to able to read what is stored in x when script1.py is run from script2.py. What I'm currently doing is essentially running part of script1.py twice. I want to know if it's possible to access the variable x without doing the following:
#script1
def read_file():
f = open('file_path.txt')
string = f.read()
return string
def main():
x = read_file()
if __name__ == '__main__':
main()
And script2.py:
#script2
import script1
from script1 import *
def main():
script1.main()
x = read_file()
print(x)
if __name__ == '__main__':
main()
So essentially I want to know how this sort of thing is supposed to be done usually. Is it possible to get the value of x without having to run read_file() a second time? When script1.main() is executed, where is the value of x stored and how can I access it? Not sure if I should be using import script1 or if I should use from script1 import * to achieve this.
Since x is a local variable in the script1.main function, it is not accessible from outside (neither from script2 nor even from script1). In fact, the object will be discarded after script1.main is finished.
If you want to reuse it, you need to store it somewhere else.
A quick fix is to make the variable x global. Then script2 can use it as script1.x.
# script1.py
def read_file():
return "foo bar"
def main():
global x
x = read_file()
if __name__ == '__main__':
main()
# script2
import script1
def main():
script1.main()
#x = read_file()
print(script1.x)
if __name__ == '__main__':
main()
If you do not want to use the global syntax, an alternative is to define a mutable variable in the script1.py and let it store the variable you want to reuse. One possibility is to use class. Here, I am using class as a mutable storage. If you do not like it, other mutable objects such as dict would also work.
# script1.py
class data:
x = None
def read_file():
return "foo bar"
def main():
data.x = read_file()
if __name__ == '__main__':
main()
# script2
import script1
def main():
script1.main()
#x = read_file()
print(script1.data.x)
if __name__ == '__main__':
main()
Using work_block function, how can I use the inspect and sys libraries to stop print_text function from printing hello?
import inspect
import sys
def print_text():
print("Hello")
def main():
work_block()
print_text()
def work_block():
# stop print "Hello" from executing
if __name__ == '__main__':
main()
You don't need special libraries:
def work_block():
# stop print "Hello" from executing
global print_text
print_text = lambda : None
But why would you want to do that?
By the way, the trailing colons when calling functions in main() produce syntax errors.
#conf.py
def init():
global mylist
mylist=[]
#change.py
import conf
def change():
if __name__ == "__main__":
print('Direct')
conf.mylist.append('Directly executed')
print(conf.mylist)
else:
conf.mylist.append('It was imported')
#exec.py
import conf
import change
conf.init()
change.change()
print (conf.mylist)
When running exec.py the result is what I expected, but when running change.py directly I didn't get any output (no Direct , no conf.mylist)
Yes, that's the normal behavior. You need to call the change function for this code to execute.
You can add the following to the end of change.py
if __name__=="__main__":
change()
This is because change is never invoked. Invoke it at the end of the file with change()
I am trying to understand the usage of #main annotation in python.
With the below python program,
def cube(x):
return x * x * x
def run_tests():
printf("Should be 1:", cube(1))
printf("Should be 8:", cube(2))
printf("Should be 27:", cube(3))
#main
def main():
print("Starting")
run_tests()
print("Ending.")
I get the following error:
PS C:\Users\MOHET01\Desktop> python.exe -i .\cube.py
Traceback (most recent call last):
File ".\cube.py", line 9, in <module>
#main
NameError: name 'main' is not defined
>>>
Function that is imported from ucb is as shown below:
def main(fn):
"""Call fn with command line arguments. Used as a decorator.
The main decorator marks the function that starts a program. For example,
interact()
#main
def my_run_function():
# function body
Use this instead of the typical __name__ == "__main__" predicate.
"""
if inspect.stack()[1][0].f_locals['__name__'] == '__main__':
args = sys.argv[1:] # Discard the script name from command line
print(args)
print(*args)
print(fn)
fn(*args) # Call the main function
return fn
My question:
Despite i define function with intrinsic name main, Why do i see this error?
I should use this:
def main():
#Do something
if __name__ == "__main__":
#Here use the method that will be the main
main()
I hope this helps
The #main decorator is implemented in a file your course provides, but you have not imported it. The page you linked says to use
from ucb import main, interact
to import the ucb.py features in your program.
As for why the error says name 'main' is not defined, that's because the function definition doesn't actually finish defining anything until the decorators execute. The reuse of the name main for both the decorator and the decorated function is confusing; the main in #main is a different function from the main you're defining in def main(): .... The main in #main is defined to run the decorated function if the file is run as a script, while the main in def main(): ... is the function to be run.
I would strongly recommend not using anything like this decorator when you don't have to. The standard way to perform the task the decorator performs is to write
if __name__ == '__main__':
whatever_function_you_would_have_put_the_decorator_on()
or if you want to handle command line arguments like the decorator would,
if __name__ == '__main__':
import sys
whatever_function_you_would_have_put_the_decorator_on(*sys.argv[1:])
The decorator is an attempt to hide the issues of sys.argv and __name__ so you don't have to know about them, but it has a problem. If you try to write something like this:
#main
def hello():
print(hello_string)
hello_string = 'Hi there.'
you'll get a NameError, because hello_string won't be assigned until after the decorator runs. If you continue to write Python beyond this course, you'll find that using if __name__ == '__main__' is less bug-prone and more understandable to other programmers than using a decorator for this.
You are using the function before it is defined. In other words, you need to define the main function higher up (in the document) than where you use it as a decorator:
def main():
pass
#main
def somefunction():
pass
The #main notation means the main function is being used to "decorate", or modify, another function. There are various articles on python decorators:
http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/
http://www.artima.com/weblogs/viewpost.jsp?thread=240808
http://www.jeffknupp.com/blog/2013/11/29/improve-your-python-decorators-explained/
You can only use a decorator on a different function. Example:
def foo(f):
def inner():
print("before")
f()
print("after")
return inner
#foo
def bar():
print("bar")
if __name__ == "__main__":
bar()
Output:
before
bar
after
I have just started learning python and am getting caught up. I come from mostly C background.
class Alarm:
def timer():
def main():
print ("Timer has Started")
main()
I always get a silly error when I try to run this code:
alarm > python alarm.py
File "alarm.py", line 5
def main():
^
IndentationError: expected an indented block
You have an empty def
def timer():
use
def timer():
pass
instead.
Learn about the pass statement, main is usually not part of the class.
A global (module level) main() function is simpler than an Alarm.main() class method. Usually, main() functions come at module level.
class Alarm:
def timer():
pass
def main():
print ("Timer has Started")
main()
I think you want to use __init__ though, which is the constructor...
class Alarm:
def timer(self):
print('timer has started')
def __init__(self):
print('constructor')
self.timer()
x = Alarm()
constructor
timer has started
My example differs from the others in that I'm actually instantiating a new object.
Notes:
specify self as the first argument to any method defined in the class
__init__ is the method to define for the constructor
invoke the class by doing variableName = className() like you would invoke a function, no new keyword
if you have an empty function, use the pass keyword like def foo(self): pass
Invoking main() will give an undefined function error, as it is a Alarm method.
IMHO the right form you should use is the following:
class Alarm:
def timer():
pass
#staticmethod
def main():
print ("Timer has Started")
if __name__ == "__main__" :
Alarm.main()
try deindent main() and add pass to timer and define an init method:
class Alarm():
def __init__(self):
print ("Timer has Started")
<shell>$ Alarm()
Your timer function is not defined. (And your space/tab indentation may be mixed)
See the tutorial (classes) for more details on classes.
class Alarm:
def timer(self):
pass
def main(self):
print ("Timer has Started")
if __name__ == '__main__':
class_inst = Alarm()
class_inst.main()
If you getting into python read PEP8.
Also, using pylint helps, it will point out indentation and many other errors you'll run across before you 'execute' your code.
As others have pointed out, you have a syntax error because timer() has no body.
You don't need to use main() in python at all. Usually people use it to indicate that the file is the top level program and not a module to be imported, but it is just by convention
You may also see this idiom
def main():
blah blah
if __name__ == "__main__":
main()
Here __name__ is a special variable. If the file has been imported it will contain the module name, so the comparison fails and main does not run.
For the top level program __name__ contains "__main__" so the main() function will be run.
This is useful because sometimes your module might run tests when it is loaded as a program but you don't want those test to run if you are importing it into a larger program
In Python, you don't need to define everything as a class. There's nothing to encapsulate in this code, so there's no reason to define an Alarm class. Just have the functions in a module.
Thanks for all the help everybody. I was making a little alarm/timer to remind me to get up and take a walk every now and then. I got most of it working, and it works great. Checked it against a stop watch and it works great.
import time
def timer(num):
seconds = num*60
print (num , "minutes", seconds , "seconds")
while (seconds > 0):
print (seconds, "seconds")
time.sleep(1)
seconds = seconds-1
print ("Time to get up and take a WALK!!!!")
main()
def main():
number = input("Input time : ")
int(number)
timer(number)
if __name__ == '__main__':
main()