Open main menu

CDOT Wiki β

Changes

OPS445 Online Lab7

323 bytes removed, 11:42, 10 March 2022
m
no edit summary
</source>
: Please notice that the function named sec_to_time() did not get moved under the class block. It remains as an external function.
:1. [https://ict.senecacollege.ca/~eric.brauer/ops445/labs/lab7/lab7d.py Download] or create Create a new python file and name it as '''lab7d.py''' and place the code listed above in it.
:2. Save the file, and test the new time object in an interactive Python shell: <source lang="bash">
[rchan@centos7 lab7]$ python3
: Try the following code. Have each in a separate Python file.
: '''[https://ict.senecacollege.ca/~eric.brauer/ops445/labs/lab7/lab7g.py lab7g.py]Local scope''' - local scope: <source lang="python">
#!/usr/bin/env python3
# Student ID: [seneca_id]
: Sometimes you want to have an object accessible from anywhere in your program, including inside and outside any functions. Here's an example:
:1. '''[https://ict.senecacollege.ca/~eric.brauer/ops445/labs/lab7/lab7h.py lab7h.py]Global scope''' - global scope : <source lang="python">
#!/usr/bin/env python3
# Student ID: [seneca_id]
:3. Python has one weird quirk when it comes to global scope: if you assign something to an existing object inside a function - it will assume you want to create a new object in that function's local scope. That will hide the global object inside the function unless you declare it explicitly with the global keyword:
:4. '''[https://ict.senecacollege.ca/~eric.brauer/ops445/labs/lab7/lab7i.py lab7i.py]Global keyword''' - global keyword : <source lang="python">
#!/usr/bin/env python3
# Student ID: [seneca_id]
572
edits