ยอดเยี่ยมมาก!

การเขียนฟังก์ชันใน Python

Shayne Miel

Software Architect @ Duo Security

บทที่ 1 - แนวปฏิบัติที่ดี

  • Docstrings
  • DRY และ Do One Thing
  • Pass by assignment (mutable vs immutable)
การเขียนฟังก์ชันใน Python

บทที่ 2 - Context Managers

with my_context_manager() as value:
  # do something
@contextlib.contextmanager
def my_function():
  # this function can be used in a "with" statement now
การเขียนฟังก์ชันใน Python

บทที่ 3 - Decorators

@my_decorator
def my_decorated_function():
  # do something
def my_decorator(func):
  def wrapper(*args, **kwargs):
    return func(*args, **kwargs)
  return wrapper
การเขียนฟังก์ชันใน Python

บทที่ 4 - Decorators เพิ่มเติม

def my_decorator(func):
  @functools.wraps(func)
  def wrapper(*args, **kwargs):
    return func(*args, **kwargs)
  return wrapper
การเขียนฟังก์ชันใน Python

บทที่ 4 - Decorators เพิ่มเติม

def decorator_that_takes_args(a, b, c):
  def decorator(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
      return func(*args, **kwargs)
    return wrapper
  return decorator
การเขียนฟังก์ชันใน Python

ขอบคุณ!

การเขียนฟังก์ชันใน Python

Preparing Video For Download...