Comments (3)
Good question. The db
variable is defined at the top of module outside of any function. This makes it a "global" variable. So, it is accessible within any function (with certain important exceptions). And, since it is a list, it can be appended and changed without needing to use the global
command in the function (as long as you don't try to erase it using a line such as db=[]
. That will cause the variable to revert to a local variable).
So, when the add_patient_to_db
function is run, and it comes to line 21 where db
is referenced. Since no new db
variable has been created in this function, Python looks to the global workspace and finds db
there, and modifies it as needed. A global variable should not be included in the parameter list of the function definition, nor should it be returned.
Then, that brings up your next question about how do you test a function when it modifies a global variable and then doesn't return it. I went over this specific test in class on Thursday. Please review the Panopto Recording from Thursday (found at https://pratt.capture.duke.edu/Panopto/Pages/Viewer.aspx?id=c4d86e48-8380-4f03-849d-acf60167251e) and look at the section starting at 12:03 of the recording. I give an example of how you can test a function that modifies a global variable.
The code for this testing example can be found in my ClassworkSpring2021 repository on the server_design
branch in a file called test_health_db_server.py
and is linked here:
https://github.com/dward2/ClassworkSpring2021/blob/eef72f529319c7c159e0179761903d4e2e3f836c/test_health_db_server.py#L1-L13
Let me know if you have any further questions or if my explanation isn't clear.
from 2021-03-18-server-code-design.
@minhuiyu0418 I'm tagging you to this issue to make sure that you were alerted to my reply. Please visit the issue and look at my previous comment.
from 2021-03-18-server-code-design.
@dward2 Thank you it's very clear!
from 2021-03-18-server-code-design.
Related Issues (1)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from 2021-03-18-server-code-design.