A. Creating function:
1. Syntax
def<function name> (<formal parameters>) :
? ? ? <function body>
? def is a keyword
? Name is any legal Python name
? Within parenthesis are zero or more formal?parameters – each is a variable name to be used inside?function body
2. some points
?Expressions for each parameter are evaluated,?bound to formal parameter names of function
?Control transfers to first expression in body of?function
?Body expressions executed until return keyword?reached (returning value of next expression) or?run out of expressions (returning None)
?Invocation is bound to the returned value
?Control transfers to next piece of code
3. docstring is a special type of comment that is used to document what your function is doing.Typically, docstrings will explain what the function expects the type(s) of the argument(s) to be, and what the function is returning.
4. Properties
– Decomposition: Break problems into modules?that are self-contained, and can be reused in?other settings
– Abstraction: Hide details. User need not know?interior details, can just use as if a black box.
5. consideration from finger problem:
-False, None, 0, 0.0, empty array usually consider as "false"
-Python it?is?legal to compare functions!
-any legal python name could be considered as function
-如果對應(yīng)function中已經(jīng)給了賦值破加,那么按照給定賦值run,如果沒有,就用local binding志鹃。
-str. lower () --->將str的大寫轉(zhuǎn)換為小寫字母
B. Specifications:"""XXXXXXX"""
1. Are a contract between implementer of?function and user
– Assumptions: conditions that must be met by?users of function. Typically constraints on?parameters, such as type, and sometimes?acceptable ranges of values
– Guarantees: Conditions that must be met by?function, provided that it has been called in way?that satisfies assumptions
c. string method
Objects are special because we can associate special functions, referred to as?object methods, with the object.?
https://docs.python.org/3/library/stdtypes.html#string-methods
http://www.greenteapress.com/thinkpython/html/thinkpython009.html#toc93