Skip to content

Latest commit

 

History

History
330 lines (168 loc) · 7.72 KB

3. Functions.md

File metadata and controls

330 lines (168 loc) · 7.72 KB

Functions

Learning plan

  • What is function?
  • Why do we need/use them?
  • What are function elements?
  • Input/output of a function?
  • Does function alternative exist?
  • Defining function VS calling function !!!
  • What types of functions exist?
  • Does function alternative exist?
  • What are function parameters?
  • What types of parameters exist?
  • What is function scope?

Good definition

  • Function is block of code that is organised and grouped together in a way so that it can perform specific task.

  • Dividing your code into functions is going to make your code reusable, easier to read and maintain.

Types of functions

  • Built-in

  • User defined

Packages

  • Think about it as folders of files.

  • Main purpose is to help better organise files within python project.

  • Alternative would be to have all functions/classes in one file, which would be difficult to change, modify or even find.

  • Module is another name for python file i.e. file with extension '.py'.

Import statement

  • Different ways to import functions/classes
    • import module_name
    • import module_name as some_alias
    • from module_name import function_name/class_name
    • from module_name import *

Function paradigme

  • A factory that takes inputs and produces outputs.
    • Inputs: arguments
    • Output: return value
  • How many values can be returned?

Why functions?

  • Easier to read and debug code

  • Can make code smaller by eliminating repetitive parts of code.

    • Rule: Write function for every part of code that you use on more than 2 places.

Function composition

  • Using output of one function as input to other function.

  • Extremely useful in functional style of programming.

Two part of function

  • Head
    • Name of function
    • Arguments
  • Body
    • What should be done?
    • What operations should be performed?
  • Body-header delimiter ‘:’

Elements of function

  • Function name

  • Function arguments

  • Sequence of steps that should be accomplished (function body)

  • Return value

Function arguments

  • Default value of arguments

  • *args

  • *kwargs

Function workflow

  • Defining function - Declaring what should be done

  • Calling function - Performing actions

  • Statements defined inside function are not performed until function is called!

Variable scope

  • Why should we care at all?
    • Passing arguments to function - meaning???
    • We can make/commit side effects i.e. we can non-intensionally modify values stored in variables.
    • We can non-intensionally change value of global variable.

Passing variables (as arguments) to functions !!!

  • Very simple concept on first hand, but with potential to make really big mess (if not understood correctly and completely.)

Pass by value/reference

  • This is separate topic. We are gonna study it in future. Today, we just want to scratch the surface!

  • a=77; b=a;

  • L = [1,2,3]; s = L;

  • When we say that sth is equal to sth i.e. when we assign value of sth to sth else, what happens under the hood? What we are actually doing?

Pass by value/reference

  • We can do one of two things:
    • We can copy element(s) from one box and put this copy to another box, or
    • We can put sth special into newly created box. This special element would be address of element that we want to copy.

Variable scope

  • Local scope
  • Enclosing scope
  • Global scope
  • Built-in scope

Variable scope - short intro

  • Function parameters are passed by object reference.

  • Inside function body we can access value of any global variables.

  • Variables declared inside function body are local

    • What does it mean local?
  • We can use global keyword to alter default behaviour

  • Global scope VS Local scope

  • Can you declare global variable from within local scope?

  • Can you access global variables from local scope? And vice verse?

    • You can access global variables from local scope.
    • You can’t access local variables from global scope.
  • Can you change global variable from local scope?

  • In other words, can operations performed in local scope affect variables in global scope?

Mutable VS Immutable

  • It depends on type of variable (object).

  • Global mutable objects can be changed from local scope, while immutable global objects can’t be changed from local scope.

  • How we can check if object is mutable or not?

  • If we can change/modify value stored in box, object is mutable.

  • How can we change/modify it?

    • Add/append elements, change value of existing elements etc.
  • Mutable-immutable check???

  • Object is mutable if at least one condition is true:

    • We can add/append elements to box or
    • We can modify existing elements from box.
  • After object modification, id() of object has not been changed!!!

  • Also, there are some methods/functions that change mutable object that is passed, while other methods/functions do not change object passed as parameter. It depends on how method is defined.

  • When we call some method, and get sth returned, then original object should NOT be unchanged. (It is general rule, should not be true in 100% of cases.)

Variable scope - Best practice

  • It is bad practice to use global variables in function body.

  • Why? -> If we change mutable object inside function (within local scope), changes will be reflected in global scope, also.

  • (Global immutable objects we can’t even change inside function body. We can only access it.)

Mutable/Immutable

  • Mutable objects:

    • List
    • Dictionary
    • Set
    • Bytearray
  • Immutable objects:

    • Integer
    • Float
    • String
    • Tuple
    • Frozen set
    • Complex
    • Bytes
  • Immutable objects are accessible in a quicker way than mutable objects.

  • Changing immutable objects is fundamentally expensive as it involves creating a copy; but changing mutable objects is considered cheap. (We see that ‘changing immutable object’ is actually incorrect name, because it is not being changed. Just copied and copy being changed.)

Pay attention !!!

  • If you access value of global mutable variable inside function body and change it -> changes will be saved to global variables.

  • If you want to access value of global mutable variable inside function body, change it, but do not want to affect state of global variable -> use copy library. (from copy import copy, deep copy)

Pass by value

For now skip. We will deal it in future separate lecture.

Just be aware that sth like that exist.

Pass by reference

  • For now skip. We will deal it in future separate lecture.

  • Just be aware that sth like that exist.

Pass by object reference

  • This is a way Python is designed.

  • Variables are always passed by object reference.

    • First, interpreter try to change ‘original object.’
    • Second, if it is mutable, change is done. If it is not mutable, then it is copied and copy is changed.

What we learned today

  • What is function?

  • Why do we need/use them?

  • What are function elements?

  • Input/output of a function?

  • Does function alternative exist?

  • Defining function VS calling function !!!

  • What types of functions exist?

  • Does function alternative exist?

  • What are function parameters?

  • What types of parameters exist?

  • What is function scope?

Exercise tips

  • How to answer questions?
    • Paraphrase question. Explain it in simple words.
    • Identify input, desired output.
    • Think what elements you can use to solve question i.e. to transform inputs into outputs.
    • Combine (encapsulate) elements into reusable unit.

The end

  • Thank you for your most valuable resource. Your time.