S206E057 -- Lecture 20, 5/25/2021, Python – an Overview

S206E057 -- Lecture 20, 5/25/2021, Python – an Overview

S206E057 – Spring 2021 S206E057 -- Lecture 20, 5/25/2021, Python – an overview Copyright © 2021, Chiu-Shui Chan. All Rights Reserved. Global and local variables: differences between the two… Global variable is usually declared at the start of the program, their global scope means that they can be used in any procedure or subroutine in the program. If the global variable has not been redefined, it will stay with the value that was originally defined without any changes. So, whenever a variable is defined, its value will stay the same unless it is modified locally. See the variable f in the following example on the following left image. Alternatively, global variable could also be declared by the format of: global f Local variable is declared within subroutines or programming blocks, their local scope means they can only be used within the subroutine or program block in which they were declared. Python deals with variables inside a function as local variables, if it is not declared outside the function. So when we define variables inside a function definition, they are local to this function by default. This means that anything we do to such a variable in the body of the function will have no effect on other variables outside of the function, even if they have the same name. Thus, the function body is the scope of a local variable. In the following example, the value of the variable f is defined as 0 first. Thus, the first print on line 7 is 0. Then, in the someFunction, f is re-defined locally as a string of “window”. This value is the local variable definition activating inside the function. When the function is called, its value is changed from 0 to a string of “window” residing within the function. After the function completes; the value of f returns back to 0 and printed by the coding on line 16. However, if the variable f is defined as global variable inside the someFunction, then it will have the following results. 0 window window It is because that f is defined as a global variable from inside the function, its value remains as in global scope after the function is completed. Please compare the output components on the left (local) and right (global) pictures. Global variable Local variable Page 1 (5/24/2021) S206E057 – Spring 2021 Note: The variable f on left is defined as 0, and redefined in somefuntion to window locally. After the function completed, its value is 0 again as a global aspect. For the example on the up right, it is seldom advisable to use global variables as they are liable to cause bugs, waste memory and can be hard to follow when tracing code. The example given on the above right is not considered as an appropriate programming style. Particularly, it resides inside a function. Other example of global and local aspect: In Python, some of the variables could be defined as global ones. See the example on the right. In this example, the FloorArea is defined as 3000 square feet, which is assumed to be the building code regulation and thus defined as global variable that won’t be changed in the whole program after this line. Users could input 3000 or above, then it will be switched to a string value of “Too Big.” After the function of BuildingCode check is completed, it will resume back to the original value of 3000. In this example, an if-then logical operation is introduced, which will be further explained in the next lecture. Summaries of global versus local variables: If you define global variables (variables defined outside of any function definition), they are visible inside all of the functions. They have global scope. It is a good programming practice to avoid defining global variables and instead to put your variables inside functions and explicitly pass them as parameters where needed. Function calls: Functions can call functions instead of just executing one function after the other. Here is the example. Two functions are defined first, then call these two separately on the left example below. On the right, we have a main function defined to call these two functions to run. Both are the same in results. But, the right one is well organized. In fact, in programming style, some professional programmers would use one major function to execute the entire task. This, in fact, sets up an execution hierarchy on implementing the entire algorithm efficiently. It also helps to apply the algorithm more user friendly when it is saved in any of open source public library. Page 2 (5/24/2021) S206E057 – Spring 2021 Function’s formal parameter and argument: Values are passing through the parentheses into the function. Here in the definition heading for functionA, person is referred to as a formal parameter. This variable name is a placeholder for the real value. In the function call, the word person between the parentheses is the argument or actual parameter. The argument supplies the actual data to be used in the function. So, when functionA is called, Python associates the formal parameter name person with the actual data John. Thus, the actual parameter value is passed to the function for print. String data from Rhino input method: One way of getting input, instead from passing the value from formal parameter into the function, is from user keyboard input. For example: def functionA(person): print(person + " Bottom Up!") def main(): userName = raw_input("First name") print('You entered ' + userName) functionA(userName) main() Here, raw_input is one of the user input functions (or mechanisms) that accepts users’ character input and returns it as a string. The prompt string of “First name” is shown on Rhino screen as a message waiting for user’s input. Then, Python accepts the input as its own value. The other mechanism is the function of “input” for getting a valid Python expression and returns the evaluated result, or getting number values as shown in the following example. The input/output results are also displayed in Rhino command area. Number data input in Rhino: A sub-function could take two arguments, passing them through the formal parameter into the statements. Inside the main function, arguments were decided by the input command that takes user input from Rhino. One example of getting integer input is the function of int() to specify input data type. • Prompt in Input([prompt]) is to prompt users with given message as a reminder. • Users could type [x*5 for x in range (2, 10, 2)], with results of [10, 20, 30, 40], details please see lecture 18, p. 6. Page 3 (5/24/2021) S206E057 – Spring 2021 • Int(x, base=10) will return (or say assign) an integer object constructed from a number or a string x, or return 0 if no argument is given by users. • The .format() method of the str type is an extremely convenient way to format text exactly the way you want it to. It will fill the value of assigned variables to the curried parentheses {}. We don’t have to apply int() function to let Python know that the input is an integer number. But, if the input number must be integer for related integer number operation, then it is appropriate to use this function for avoiding unexpected errors. Here is another example with a print function instead of print a variable. The result is ‘None’. It is because inside the print call, it calls the function to print the sentence and stop. That function had not returned anything explicitly for print to do, so it prints none to show nothing. This notion relates to the data output phenomenon. Data output and build-in name: Inside a function, if there is a statement of return [expression] is defined and encountered, then it exits a function, and optionally passing back an expression to the caller. A return statement with no arguments is the same as return “None”. The following one is a simplest form of a Python function. This function takes a string as input parameter and prints it on standard screen. def printme (stir): stir = "This prints a passed string into this function." print stir return (stir) print (printme(stir)) Here, “stir” is a variable. If you write the code in your program and it doesn’t work due to the name “stir” is not defined, then you have to declare a value for the variable stir first to let Python recognize this defined variable. See the example on the right. Yet, change the name to str, then Python recognizes it as a string and works. See the example shown on the previous page. But, str is a built-in name of string function. Page 4 (5/24/2021) S206E057 – Spring 2021 It has been explained in many articles that in Python programming convention, we would never use built-in names as variable identifiers (variable names). Of course, it won’t confuse the program interpreter but it may confuse people reading your code. Uncecessary use of built-in names for attributes and methods should be avoided. Another ill-effect is that shadowing built-ins confuses syntax highlighters in most python-aware editors (vi, emacs, pydev etc). Some of the editor tools will warn about this practice. What happen if the return statement occurs before the print statement? See the comment part of the return function above. Local Scope of variables inside a function: For the logic of writing functions, it is important that the writer of a function knows the names of variables inside the function. On the other hand, if you are only using a function, maybe written by someone unknown to you, you should not care what names are given to values used internally in the implementation of the function you are calling.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us