DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

Function in Python (2)

Buy Me a Coffee

*Memo:

One or more pass statements, return statements or values can be used to do nothing in a function, returning None as shown below:

*Memo:

  • Basically, a pass statement is used for the function to do nothing.
  • The function with no code gets error.
def func():
    pass
    pass
    return
    return
    100
    200
    "Hello"
    "World"

print(func())
# None
Enter fullscreen mode Exit fullscreen mode
def func(): # SyntaxError: incomplete input
    # No code
Enter fullscreen mode Exit fullscreen mode

A class and function can be passed to a function as shown below:

class mycls:
    v = "Hello"

def myfn():
    return "World"

def func(cls, fn):
    print(cls.v, fn())

func(mycls, myfn)
# Hello World
Enter fullscreen mode Exit fullscreen mode

A class and function can be defined in a function as shown below:

def func():
    class mycls:
        v = "Hello"
    def myfn():
        return "World"
    print(mycls.v, myfn())

func()
# Hello World
Enter fullscreen mode Exit fullscreen mode
# 3D function
def func(num):
    def func(num):
        def func(num):
            return num**2
        return func(num)
    return func(num)

print(func(3))
# 9
Enter fullscreen mode Exit fullscreen mode

A function can be written in one line as shown below:

def func(num1, num2): return num1+num2

print(func(3, 5))
# 8
Enter fullscreen mode Exit fullscreen mode
def func(): return

print(func())
# None
Enter fullscreen mode Exit fullscreen mode
def func(): pass

print(func())
# None
Enter fullscreen mode Exit fullscreen mode

A function can be indirectly assigned to a variable but cannot be directly assigned to a variable like JavaScript except a lambda as shown below:

def func(num1, num2):
    return num1+num2

v = func
print(v(3, 5))
# 8
Enter fullscreen mode Exit fullscreen mode
v = def func(num1, num2):
        return num1+num2
# SyntaxError: invalid syntax

v = func(num1, num2):
        return num1+num2
# SyntaxError: invalid syntax
Enter fullscreen mode Exit fullscreen mode
v = lambda num1, num2: num1+num2
print(v(3, 5))
# 8
Enter fullscreen mode Exit fullscreen mode

The name of a function or parameter:

  • can have letters, _ and the digits except for the 1st character.
  • can be a reserved soft keyword.
  • cannot start with a digit.
  • cannot be a reserved keyword.
def True_100(True_100): pass
def tRuE_100(tRuE_100): pass
def _True100(_True100): pass
def True100_(True100_): pass
def match(match): pass
def case(case): pass
def type(type): pass
def _(_): pass
# No error

def True-100(True-100): pass
def 100_True(100_True): pass
def True(True): pass
def class(class): pass
def def(def): pass
# Error
Enter fullscreen mode Exit fullscreen mode

A function name should be lower_snake_case as shown below:

var = 'abc'
my_var = 'abc'
my_first_var = 'abc'
Enter fullscreen mode Exit fullscreen mode
def func(): pass
def my_func(): pass
def my_first_func(): pass
Enter fullscreen mode Exit fullscreen mode

Top comments (0)