Difference between revisions of "Python:Functions"

From wiki
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[category:Python]]
 
[[category:Python]]
  
You can pass a function-name as paramter
+
;sys._getframe(  ).f_code.co_name
 +
:Return name of current function
 +
 
 +
;You can pass a function-name as parameter
  
 
<syntaxhighlight lang='python'>
 
<syntaxhighlight lang='python'>
 
def main():
 
def main():
     outcome = function1('function2',par2)
+
     outcome = function1(function2,inpar2 = <value>)
    function(par2 = <value>, par1 = <value>)
 
 
     return
 
     return
  
Line 13: Line 15:
 
     return result
 
     return result
  
def function2 (inpar1, inpar2 = <defaultvalue>):
+
def function2 (inpar):
 +
    codeblock
 +
    return result
 +
 
 +
main()
 +
</syntaxhighlight>
 +
 
 +
;eval(functionnameasstring)()
 +
:Convert a function name as string to a function call
 +
:WARNING: Make sure you have control over the string provided, you may call anything this way.
 +
 
 +
 
 +
=Self defined functions=
 +
 
 +
Arguments are positional but can be passed by keyword too.
 +
 
 +
<syntaxhighlight lang='python'>
 +
def main():
 +
    outcome = function(par1,par2)
 +
    function(par2 = <value>, par1 = <value>)
 +
    return
 +
 
 +
def function (inpar1, inpar2 = <defaultvalue>):
 
     codeblock
 
     codeblock
 
     return result
 
     return result
Line 21: Line 45:
  
  
=Build in function=
+
;def function (*args,**kwargs):
 +
:Take any number of positional arguments and put them in the [[Python:DataTypes#Tuple|tuple]] args
 +
:Take any number of keyword arguments and put them in the [[Python:DataTypes#Dictionary_or_dict|dict]] kwargs
 +
 
 +
 
 +
=Anonymous functions(lambda)=
 +
lambda defines an anonymous function.
 +
 
 +
 
 +
=Build in functions=
  
 
List of all build in functions [https://docs.python.org/3/library/functions.html]
 
List of all build in functions [https://docs.python.org/3/library/functions.html]
Line 33: Line 66:
 
;min(obj1)
 
;min(obj1)
 
Return the smallest item in obj1
 
Return the smallest item in obj1
 +
 +
;round(x,y)
 +
:Round x to y decimals
  
 
;input([prompt])
 
;input([prompt])
Line 44: Line 80:
 
:Return a string representation of an object. str for the informal representation (just make it printable), repr for the formal representation (more formating??)
 
:Return a string representation of an object. str for the informal representation (just make it printable), repr for the formal representation (more formating??)
  
=Self defined functions=
+
=Boolean functions=
 
+
Functions that just return True or False
Arguments are positional but can be passed by keyword too.
+
;isinsantce(var, type)
 
+
:If a variable has the wanted type (list,set,int,str,dict)
<syntaxhighlight lang='python'>
+
;isfile(filename)
def main():
+
:If a file exists
    outcome = function(par1,par2)
+
;any(alist)
    function(par2 = <value>, par1 = <value>)
+
:If any of the elements is True (any iterateble will do)
    return
+
;any(x in alist for x in aset)
 
+
:If any of the elements of list are in set
def function (inpar1, inpar2 = <defaultvalue>):
+
;all()
    codeblock
+
:If all of the elements are true
    return result
 
  
main()
+
=Other often used functions=
 +
;Sleep function
 +
<syntaxhighlight lang=python>
 +
import time
 +
time.sleep(<sec>)
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
;def function (*args,**kwargs):
 
:Take any number of positional arguments and put them in the [[Python:DataTypes#Tuple|tuple]] args
 
:Take any number of keyword arguments and put them in the [[Python:DataTypes#Dictionary_or_dict|dict]] kwargs
 

Latest revision as of 11:11, 16 August 2023


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()
eval(functionnameasstring)()
Convert a function name as string to a function call
WARNING: Make sure you have control over the string provided, you may call anything this way.


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()


def function (*args,**kwargs)
Take any number of positional arguments and put them in the tuple args
Take any number of keyword arguments and put them in the dict kwargs


Anonymous functions(lambda)

lambda defines an anonymous function.


Build in functions

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

round(x,y)
Round x to y decimals
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??)

Boolean functions

Functions that just return True or False

isinsantce(var, type)
If a variable has the wanted type (list,set,int,str,dict)
isfile(filename)
If a file exists
any(alist)
If any of the elements is True (any iterateble will do)
any(x in alist for x in aset)
If any of the elements of list are in set
all()
If all of the elements are true

Other often used functions

Sleep function
import time
time.sleep(<sec>)