COMP10001 Lecture Notes - Lecture 6: Call Stack, Defensive Programming, Stack Trace

58 views2 pages
If mutable object is passed to a function and locally mutated, the change is preserved in the
global object
-
Consistent with the behaviour of mutable objects user assignment/mutation
-
Functions and mutability:
Parameters = name that appear in a function definition
-
Arguments = values passed to a function when calling
-
Parameters can be given default arguments
-
Default argument values are evaluated once at the point of function definition in the defining
scope
-
Default arguments must follow non
-
default arguments
-
Arguments can be matched to parameters by position or by parameter names
-
Parameters and arguments:
If the function has the answer, return straight away
-
Faster
-
Returning early:
A namespace is a mapping from names to objects
-
Global namespace in Python
-
Local namespace is called when function is called and forgotten after the function ends
-
Scope is the area of Python code where a particular namespace is used
-
e.g.
-
a = 3
def f(x):
i = 2
return x+i
Global namespace contains a, f and b
Local namespace of f contains x and i
b = 6
Python first looks in the local namespace, then enclosing function namespaces and then in the
global namespace
-
dir() lists the current namespace
-
Don't use the same parameter names in sub
-
functions
-
Avoid global variables wherever possible
-
Capitalise constants
-
Namespaces
Week 6
Sunday, 15 April 2018 6:42 PM
Foundations of Computing Page 1
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Document Summary

If (cid:373)uta(cid:271)le o(cid:271)je(cid:272)t is passed to a fu(cid:374)(cid:272)tio(cid:374) a(cid:374)d lo(cid:272)all(cid:455) (cid:373)utated, the (cid:272)ha(cid:374)ge is p(cid:396)ese(cid:396)(cid:448)ed i(cid:374) the glo(cid:271)al o(cid:271)je(cid:272)t. Co(cid:374)siste(cid:374)t (cid:449)ith the (cid:271)eha(cid:448)iou(cid:396) of (cid:373)uta(cid:271)le o(cid:271)je(cid:272)ts use(cid:396) assig(cid:374)(cid:373)e(cid:374)t/(cid:373)utatio(cid:374) Pa(cid:396)a(cid:373)ete(cid:396)s = (cid:374)a(cid:373)e that appea(cid:396) i(cid:374) a fu(cid:374)(cid:272)tio(cid:374) defi(cid:374)itio(cid:374) A(cid:396)gu(cid:373)e(cid:374)ts = (cid:448)alues passed to a fu(cid:374)(cid:272)tio(cid:374) (cid:449)he(cid:374) (cid:272)alli(cid:374)g. Default a(cid:396)gu(cid:373)e(cid:374)t (cid:448)alues a(cid:396)e e(cid:448)aluated o(cid:374)(cid:272)e at the poi(cid:374)t of fu(cid:374)(cid:272)tio(cid:374) defi(cid:374)itio(cid:374) i(cid:374) the defi(cid:374)i(cid:374)g s(cid:272)ope. A(cid:396)gu(cid:373)e(cid:374)ts (cid:272)a(cid:374) (cid:271)e (cid:373)at(cid:272)hed to pa(cid:396)a(cid:373)ete(cid:396)s (cid:271)(cid:455) positio(cid:374) o(cid:396) (cid:271)(cid:455) pa(cid:396)a(cid:373)ete(cid:396) (cid:374)a(cid:373)es. If the fu(cid:374)(cid:272)tio(cid:374) has the a(cid:374)s(cid:449)e(cid:396), (cid:396)etu(cid:396)(cid:374) st(cid:396)aight a(cid:449)a(cid:455) A (cid:374)a(cid:373)espa(cid:272)e is a (cid:373)appi(cid:374)g f(cid:396)o(cid:373) (cid:374)a(cid:373)es to o(cid:271)je(cid:272)ts. Lo(cid:272)al (cid:374)a(cid:373)espa(cid:272)e is (cid:272)alled (cid:449)he(cid:374) fu(cid:374)(cid:272)tio(cid:374) is (cid:272)alled a(cid:374)d fo(cid:396)gotte(cid:374) afte(cid:396) the fu(cid:374)(cid:272)tio(cid:374) e(cid:374)ds. S(cid:272)ope is the a(cid:396)ea of p(cid:455)tho(cid:374) (cid:272)ode (cid:449)he(cid:396)e a pa(cid:396)ti(cid:272)ula(cid:396) (cid:374)a(cid:373)espa(cid:272)e is used e. g. a = (cid:1007) def f(cid:894)(cid:454)(cid:895): i = (cid:1006) (cid:396)etu(cid:396)(cid:374) (cid:454)+i (cid:271) = (cid:1010) Lo(cid:272)al (cid:374)a(cid:373)espa(cid:272)e of f (cid:272)o(cid:374)tai(cid:374)s (cid:454) a(cid:374)d i.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents