Difference between revisions of "Python:Functions"
Jump to navigation
Jump to search
Line 1: | Line 1: | ||
[[category:Python]] | [[category:Python]] | ||
+ | |||
+ | ;sys._getframe( ).f_code.co_name | ||
+ | :Return name of current function | ||
You can pass a function-name as parameter | You can pass a function-name as parameter | ||
Line 5: | Line 8: | ||
<syntaxhighlight lang='python'> | <syntaxhighlight lang='python'> | ||
def main(): | def main(): | ||
− | outcome = function1( | + | outcome = function1(function2,inpar2 = <value>) |
− | |||
return | return | ||
Line 13: | Line 15: | ||
return result | return result | ||
− | def function2 ( | + | def function2 (inpar): |
codeblock | codeblock | ||
return result | return result |
Revision as of 20:23, 14 April 2021
- sys._getframe( ).f_code.co_name
- Return name of current function
You can pass a function-name as parameter
def main():
outcome = function1(function2,inpar2 = <value>)
return
def function1 (functiontocall, inpar2 = <defaultvalue>):
result = functiontocall(inpar2)
return result
def function2 (inpar):
codeblock
return result
main()
Build in function
List of all build in functions [1]
- len(obj1)
- Return the number of elements in obj1 (string, list, tuple, set)
- max(obj1)
- Return the largest item in obj1
- min(obj1)
Return the smallest item in obj1
- input([prompt])
- Read and return input from standard input. Display 'prompt' first when provided.
- int(string[,base=x)])
- Convert str to an integer. Base = 2 converts bit-string presentation ('010101').
- str(object)
- repr(object)
- Return a string representation of an object. str for the informal representation (just make it printable), repr for the formal representation (more formating??)
Other often used functions
- Sleep function
import time
time.sleep(<sec>)
Self defined functions
Arguments are positional but can be passed by keyword too.
def main():
outcome = function(par1,par2)
function(par2 = <value>, par1 = <value>)
return
def function (inpar1, inpar2 = <defaultvalue>):
codeblock
return result
main()