Exam 3 Flashcards

1
Q

Benefits of Functions

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Definition of Functions

A

Functions in Python are defined with the keyword _def_ and can be named using the identifier naming rules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Shallow Copy

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Deep Copy

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Volatile

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Persistent

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Open

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

close, write, read

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

readline, tell, seek

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

rename, remove

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

‘os’ (og system) moduleperatin

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Attributes of Classes and Objects

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Attributes of Functions 1/3

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Attributes of Functions 2/3

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Attributes of Functions 3/3

A
  • Function-level Scope - Variables in some languages like C++, C#, and Java have block-level scope. Python uses function-level scope which means variable visibility is limited to the function containing the variable. If a variable is not in a function, it is visible within the entire module. PHP, Python, and Javascript use function-level scope.
  • Global Variables - Variables created outside of all functions have module-level ( global) scope and are visible to all code in that module. Any function within the module can accessthe global variables.
  • Global Keyword - To change global variables within a function use the global keyword within the function.
  • Shadow Variables - If the global keyword is not used within the function, the function local variables with the same name as global variables will shadow the global variables and will be local to the function.
  • Keyword Arguments - Functions can be called with keyword arguments which use the same names in the argument list as those in the parameter list. When using keyword arguments, the arguments do not have to be in the same order as the parameters.
17
Q
A