Modules Flashcards

1
Q

Write a code that will import a specific function add_numbers from a library adding_library?

A
import add_numbers from adding_library
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are Python Modules?

A

At its core, a module is just a Python file with code in it. You can import entire files or specific functions from those files for use in other applications.

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

How to create and use modules?

A
  • Simply write the code you want and save it in a .py file.
  • You can then use import to access functions in that file from other files by using the syntax from import
from <filename without the .py extension> import <function name>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a possible complication from importing a lot of code?

A

Name Dublication
* Importing code further complicates things because you will be importing names into your code that maybe you would have wanted to use yourself.

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

Explain Namespace

A
  • System to help make the names of functions and variables unique
  • Prevent duplication across modules and packages.

Python namespaces include the following:

  • built-in namespace: contains all built-in functions and exceptions.
  • global namespace: contains all the names of variables and functions you create in your program that exist outside of functions
    * local namespace: contains only names within a given function.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Importing the floor function form the maths module, in which namespace would it be?

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

How to avoid duplicate names?

A
  • By importing entire library
  • Variables and functions in library are now accessed with dot notation
  • Now: having exported variable in own global namespace, thus eliminating conflict with library function of the same name
import math
math.floor(6.5)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

There is a function, my_function. Within that function, a variable called my_var is created. In which namespace does my_var exist?

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

Python’s int( ) function will take an argument and convert it to an integer. In which namespace does int( ) exist?

A

Build-In

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

How to improve documentation

A
  • Properly named functions
  • Informative comments
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a docstring and how to use it. Give example

A
def add_two_numbers(a, b):
    """ Adds two numbers together and returns the result. """

Or over various Lines

"""
text
tect
text
"""

Accessed like this

help(add_two_numbers)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly