Skip to content

Learning Python: Day 22

Importing specific functions

  • syntax for importing a specific function from a module

from module_name import function_name

  • separate each function with a comma

from module_name import function_0, function_1, function_2

  • import a function from the making_pizzas.py example

from pizza import make_pizza

make_pizza(16, ‘pepperoni’)

make_pizza(12, ‘mushrooms’, ‘green peppers’, ‘extra cheese’)

  • because we’ve explicitly imported the function make_pizza() in the import statement, we don’t need to use the dot notation

Using as to give a function an alias

  • alias – an alternate name similar to a nickname can be used if the name fo a function you’re importing might conflict with an existing name in your program
  • as – keyword renames a function using the alias you provide

from pizza import make_pizza as mp

mp(16, ‘pepperoni’)

mp(12, ‘mushrooms’, ‘green peppers’, ‘extra cheese’)

  • function make_pizza() renamed to mp in this program
  • avoids confusion with another make_pizza() function in this program
  • syntax for providing an alias

from modeul_name import function_name as fn

Using as to give a module an alias

  • can provide an alias for the module name
  • calling p.make_pizza() is more concise than calling pizza.make_pizza()

import pizza as p

p.make_pizza(16, ‘pepperoni’)

p.make_pizza(12, ‘mushrooms’, ‘green peppers’, ‘extra cheese’)

  • module’s functions retain their original names
  • descriptive function names are more important the your code for the full module name
  • syntax

import module_name as mn

Importing all functions in a module

  • asterisk (*) operator tells Python to import every function in a module

from pizza import*

make_pizza(16, ‘pepperoni’)

make_pizza(12, ‘mushrooms’, ‘green peppers’, ‘extra cheese’)

  • because the asterisk in the import statement copied every function from the module pizza into this program file, you can call each function without using the dot notation
  • don’t use this approach when working with larger modules you didn’t write
  • best approach is to import the functions you want or import the entire module and use the dot notation

from module_name import*

Styling functions

  • functions should have descriptive names with lowercase letters and underscores
  • every function should have a comment right after the function definition and use the docstring format to explain what the function does
  • if you specify a default value for a parameter, no spaces should be used on either side of the equal sign

def function_name(parameter_0, parameter_1=’default value’)

  • the same convention should be used for keyword arguments in function calls

function_name(value_0, parameter_1=’value’)

  • pep 8 recommends limiting lines of code to 79 characters
  • press enter after the opening parenthesis on the definition line if a set of parameters causes a function’s definition to be longer than 79 characters
  • press the tab key twice on the next line to separate the list of arguments from the body of the function, which will only be indented one level
  • most editors automatically line up any additional lines of arguments to match the indentation established in the first line

def function_name(

……..parameter_0, parameter_1, parameter_2,

……..parameter_3, parameter_4, parameter_5):

….

  • can separate each function by two blank lines for legibility
  • import statements should be written at the beginning of a file, except if you use comments at the beginning to describe the program

Summary

  • learned how to write functions and pass arguments so functions can access the information they need to work
  • learned to use positional and keyword arguments and how to accept an arbitrary number of arguments
  • saw functions display output and ones that return values
  • learned how to use functions with lists, dictionaries, if statements, and while loops
  • saw how to store your functions in separate files called modules, so files will be easier to understand
  • learned to style your functions for clarity
  • functions allow you to write code once and then reuse that code
  • if you modify a function’s behavior by modifying one block of code, your change takes effect everywhere you’ve called that function

End of study session.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *