Exam 3 Flashcards
Benefits of Functions
- Modularity - Functions provide a way for developers to modularize code. By modularizing, developers obtain the benefits of improved designs and implementations.
- Reuse - Developers can call functions multiple times and from multiple locations within the same program. Developers can also import modules containing functions that can be reused by multiple programs.
- Reduce Duplication - Since the same function can be called (used) multiple times, there is no need for the same code to appear in multiple locations.
- Maintainability - It is much easier to maintain code that has been modularized using functions. The alternative to functions is having the code in larger, interrelated segments that are difficult to design, debug, and maintain.
- Information Hiding - The details of function internals are hidden to the callers of the functions. The callers just need to know the contract (or interface agreement) to use the function. That is, what arguments should be passed to the function and what data is expected for the function to return.
Definition of Functions
Functions in Python are defined with the keyword _def_ and can be named using the identifier naming rules.
Shallow Copy
With a shallow copy, only ‘top-level’ contents of the source list are copied into the destination list.
With a shallow copy, the contents of the ‘1st_item’ and ‘3rd_item’ objects are copied but the address of the embedded list is copied.
The result of this is that, after the copy, if top_level objects are changed in either the source or destination list, the other list will not be changed. However, if an element of the embedded list is changed in either the source list or the destination list, the embedded list in the other list will also change. This happens because the embedded list addresses are the same.
If either of the lists (source or destination) changes the embedded list itself (and not just an element within the embedded list) then a new object is created for that embedded list. Since an entirely new object was created for one of the embedded lists, they no longer share the common address of the original object in the list.
Deep Copy
The deep copy copies all content and not the addresses. After a deep copy, there is no association between the source and destination lists. Therefore, if either list or the embedded lists are changed, the change will have no effect on the other list (or the embedded lists).
- from copy import deepcopy
Volatile
RAM used by a program is released back to the operating system when the program terminates and all data stored there is lost. Data in RAM is volatile - which means it is lost when the program ends.
Persistent
Data stored in files is persistent. That is, data in files remains viable, or accessible, even after the program has terminated. The original program, or another program, can access data stored in a file days, months, or years after it was created.
Open
Use the open() method to open files. The open() method returns a file object that can then be used to read/write to a file.
The method has the following general case:
open(file_name, [access_mode][,buffering]).
- The file_name is the name of the file that you want to open.
- See the table below for the access_mode options.
- The ‘r’ access_mode is the default.
- Buffering determines the buffering size when accessing a file. If buffering is set to 1, 1 line will be buffered. If set to 0, no buffering takes place. If set to a negative value, buffering size is set by the system which is the default if no buffering value is supplied.
Both of the access_mode and buffering are optional.
close, write, read
- close - Use the close() method to flush any unwritten content from the file buffer and close the file object.
- write - The write(string) method writes data (string) to a file.
- read - The read([count]) method reads data from a file. If ‘count’ is supplied, then that number of bytes will be read. Without count, the default is to attempt to read until the end of file.
readline, tell, seek
- readline - The readline([count])) method reads a line from a file. If ‘count’ is supplied, then a maximum of that number of bytes will be read.
- tell - The tell() method reports the current file pointer position in a file.
-
seek- Use the seek(offset[,from]) method to change the file pointer position. ‘offset’ is the number of bytes to move. The settings for ‘from’ are:
- If = 0 then the beginning of the file is the reference point
- If = 1 then the current position is the reference point
- If = 2 then the end of the file is the reference point
rename, remove
- rename - The rename(current_name, new_name) method changes a file’s name from current to new.
- remove - The remove(file_name) method deletes the file.
‘os’ (og system) moduleperatin
Methods from the ‘os’ (operating system) module can be used to work with directories. Include the ‘import os’ statement to use these methods.
- mkdir - The mkdir(“new_directory”) method makes a directory named by the string supplied.
- chdir - The chdir(“/users/bob/new_directory”) method changes the current working directory.
- getcwd -fsm The getcwd() method returns the current working directory.
- rmdir- The rmdir(“/users/bob/old_directory”) method removes the directory supplied.
Attributes of Classes and Objects
- Class - describes the details of the objects that will be built based on the class.
- Class Identifiers - follow the naming convention of using uppercase for the starting letters of the words. Also, class names are usually nouns (e.g. Aircraft, Employee, Student, Faculty).
- Multiple Classes - A program can use multiple classes.
- Instantiation - When an object is created from a class it is known as instantiation. The object is instantiated from, or based on, a class.
- Instance - of the class is created and it is known as an object.
- Object Identifiers - follow the naming convention of variables (lowercase; underscores to separate words).
- Multiple Objects - can be instantiated from each class.
- Functionality and Data - Objects contain functionality and data. Methods represent the functionality in an object. Data members represent the data in an object.
- Constructor (Initializer) - The method named ‘__init__‘ is known as the constructor or initializer. It is called automatically when an object is instantiated.
- Member Access - access object methods and data using the dot ’.’ operator. For example, ‘child_object.set_attribute(200)’ is calling the ‘set_attribute()’ method of the ‘child_object’.
- Inheritance - Classes can inherit functionality and data from other classes.
- Getter - (accessor) member functions ‘get’ (return) data from objects.
- Setter - (mutator) member functions ‘set’ data in objects.
- Class Variables - are declared in the class but outside of all methods and are shared by all objects instantiated of that class.
- Instance Variables - are variables declared inside methods of the class. Each object of the class gets its own copy of an instance variable.
- Reduce Visibility to Variable - To make an instance variable visible only to members of the class use the double underscore prefix (e.g. self.__job_title).
- Self - instance methods must specify an object identifier that is used to reference instance variables. This object is usually called ‘self’ although it can be any identifier. Observe in the example below how ‘self’ is used to refer to the instance variables: self.name, self.__job_title. The ‘self’ object refers to the object that is calling the method.
Attributes of Functions 1/3
- Definition of Functions - Functions in Python are defined with the keyword def and can be named using the identifier naming rules.
- Function Naming Rules - Function identifiers follow the Python identifier naming rules.
- Function Naming Conventions - follow the naming convention of all lower case with words separated by underscores.
- Function Identifiers - are usually action-based since functions perform actions (e.g. set_id(), get_name(), calculate_area() are all action-based names).
- Calling Functions - Functions are called (invoked) by using the function name followed by parentheses. For instance, ‘execute_query()’ calls or invokes the ‘execute_query()’ function.
Attributes of Functions 2/3
- Argument Passing - Objects can be passed to functions via a parameter list in the function. For instance, in the line ‘def calculate_sphere_volume(radius)’ radius is a parameter in the function parameter list. A parameter receives a value from an argument when the function is called. Arguments are passed to functions when the function is called by a line like ‘calculate_sphere_volume(33)’. The number 33 is an argument in the function call argument list.
- Immutable Argument - (e.g. numeric literal, string, tuple) passed to a function will not be changed in the caller.
- Mutable Argument - passed to a function will be changed in the caller if it is changed in the called function. However, if a copy of the mutable argument is passed to the called function then the change made in the called function will not be seen in the caller.
- Multiple Parameters - If a function has multiple parameters in the function definition, then the same number of arguments should be in the call to the function.
- Local Variables - Variables created inside a function have scope which is local to that function. This means that those variables are not visible and cannot be used outside of that function.