ev3编程 python_乐高 EV3 高级编程 - 第四课:Python 模块
譯者按:使用 ev3dev Linux 系統并用 Python 編程的人數比例很低,好像這一課這樣寫 Python 編程的就更少了,你會發現程序的重用率會大大的提高!
EV3: Lesson 4 - Python Modules
EV3:第 4 課 - Python 模塊
4.1 Separate Functions, Main Program and Global Variables
4.1 分開的函數,主程序,和全局變量
In our last lesson, we learned how to use functions to perform repeated jobs. However, if the main program contains too many functions, the program size will become too large to be maintained. So, we'll need some technique to separate those functions with the main program.
在我們的上一課,我們學習了如何使用函數去執行一些重復的工作。但是,如果主程序里有太多的函數,程序會變得太大而難以維護。所以,我們需要一些技巧去分開主程序和函數。
Those python program files that stored functions are called "Python Modules".
我們叫那些儲存函數的檔案為【Python 模塊】。
In order to share variables between different python programs and modules, we'll also need to put the global variables to a seperate python module. (Note: global variables can only be used within the same python program)
為了可以讓一些變量可以同時被主程序及函數使用,我們也需要把這些全局變量放到一個分開的 Python 模塊去聲明。(要注意的是,前面課程提到的全局變量,是只能在同一個程序檔案里使用,這一課教的方法,讓全局變量可以跨檔案使用)
The following python programs and modules perform the same job that is described in lesson 3:
下面的幾個 Python 程序,會執行和第 3 課一樣的工作:
To acheive this purpose, let's create an empty python file called "g.py" that stores all required global variables:
要達到這個目的,讓我們創建一個空的 Python 程序,并命名為“g.py”,這個模塊儲存里所有需要的全局變量:
global intResult
intResult = 0
Then, we put all the functions into another python file called "fun.py"
然后,我們把所有的函數放進去另外一個 Python 模塊“fun.py”
# import g.py so that the variables defined in it can be used in this module
import g
def funAddWithReturn(intNo1, intNo2):
intTemp = intNo1 + intNo2
return intTemp
def funAddWithoutReturn(intNo1, intNo2):
# Please note that intResult has been changed to g.intResult
g.intResult = intNo1 + intNo2
def funAddWithoutReturnWrong(intNo1, intNo2):
intResult = intNo1 + intNo2
Then, we copy other contents from lesson3_03.py to lesson4_01.py with some modifications:
然后,我們把其他程序內容從 lesson3_03.py 拷貝到 lesson4_01.py 里,并稍微修改:
#!/usr/bin/env python3
from ev3dev.ev3 import *
from time import sleep, time
import traceback
# import g.py and fun.py
import g
import fun
try:
#The main program starts here
lcd = Screen()
lcd.clear()
# Declare local variable
intA = 3
intB = 5
# The answer for the following statement is 8
# Important Note ********** "fun." is added before the function name, as it is defined there!!! **********
lcd.draw.text((5, 5), "intA + intB = " + str(fun.funAddWithReturn(intA, intB)))
# The answer for the following statement is 0, pls. note that intResult has been changed to g.intResult
fun.funAddWithoutReturnWrong(intA, intB)
lcd.draw.text((5, 20), "intA + intB = " + str(g.intResult))
# The answer for the following statement is 8, pls. note that intResult has been changed to g.intResult
fun.funAddWithoutReturn(intA, intB)
lcd.draw.text((5, 35), "intA + intB = " + str(g.intResult))
lcd.update()
sleep(10)
except:
# If there is any error, it will be stored in the log file in the same directory
logtime = str(time())
f=open("log" + logtime + ".txt",'a')
traceback.print_exc(file=f)
f.flush()
f.close()
Upload the above 3 programs and run lesson4_01.py in the EV3 brick, you'll see exactly the same result as in lesson 3:
把上面的 3 個程序上傳到 EV3,并且運行主程序 lesson4_01.py,你會發現屏幕輸出會和第 3 課的一模一樣:
Figure 4.1
In our next lesson, we'll learn how to control the buttons on the EV3 brick.
在下一課,我們會學習如何控制 EV3 主機上面的按鍵。
總結
以上是生活随笔為你收集整理的ev3编程 python_乐高 EV3 高级编程 - 第四课:Python 模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自动驾驶汽车电子电气架构技术开发
- 下一篇: 电脑(PC)端独立截图软件