Decorators और metadata

Python में Functions लिखना

Shayne Miel

Software Architect @ Duo Security

def sleep_n_seconds(n=10):
  """n सेकंड तक प्रोसेसिंग रोकें.

  Args:
    n (int): जितने सेकंड रुकना है.
  """
  time.sleep(n)

print(sleep_n_seconds.__doc__)
n सेकंड तक प्रोसेसिंग रोकें.

  Args:
    n (int): जितने सेकंड रुकना है.
Python में Functions लिखना
def sleep_n_seconds(n=10):
  """n सेकंड तक प्रोसेसिंग रोकें.

  Args:
    n (int): जितने सेकंड रुकना है.
  """
  time.sleep(n)

print(sleep_n_seconds.__name__)
sleep_n_seconds
print(sleep_n_seconds.__defaults__)
(10,)
Python में Functions लिखना
@timer
def sleep_n_seconds(n=10):
  """n सेकंड तक प्रोसेसिंग रोकें.

  Args:
    n (int): जितने सेकंड रुकना है.
  """
  time.sleep(n)

print(sleep_n_seconds.__doc__)


print(sleep_n_seconds.__name__)
wrapper
Python में Functions लिखना

timer डेकोरेटर

def timer(func):
  """एक डेकोरेटर जो बताता है कि function चलने में कितना समय लगा."""

  def wrapper(*args, **kwargs):
    t_start = time.time()

    result = func(*args, **kwargs)

    t_total = time.time() - t_start
    print('{} took {}s'.format(func.__name__, t_total))

    return result

  return wrapper
Python में Functions लिखना
from functools import wraps

def timer(func): """एक डेकोरेटर जो बताता है कि function चलने में कितना समय लगा.""" @wraps(func) def wrapper(*args, **kwargs):
t_start = time.time() result = func(*args, **kwargs) t_total = time.time() - t_start print('{} took {}s'.format(func.__name__, t_total)) return result
return wrapper
Python में Functions लिखना
@timer
def sleep_n_seconds(n=10):
  """n सेकंड तक प्रोसेसिंग रोकें.

  Args:
    n (int): जितने सेकंड रुकना है.
  """
  time.sleep(n)

print(sleep_n_seconds.__doc__)
n सेकंड तक प्रोसेसिंग रोकें.

  Args:
    n (int): जितने सेकंड रुकना है.
Python में Functions लिखना
@timer
def sleep_n_seconds(n=10):
  """n सेकंड तक प्रोसेसिंग रोकें.

  Args:
    n (int): जितने सेकंड रुकना है.
  """
  time.sleep(n)

print(sleep_n_seconds.__name__)
sleep_n_seconds
Python में Functions लिखना

मूल function तक पहुँच

@timer
def sleep_n_seconds(n=10):
  """n सेकंड तक प्रोसेसिंग रोकें.

  Args:
    n (int): जितने सेकंड रुकना है.
  """
  time.sleep(n)

sleep_n_seconds.__wrapped__
<function sleep_n_seconds at 0x7f52cab44ae8>
Python में Functions लिखना

अभ्यास करते हैं!

Python में Functions लिखना

Preparing Video For Download...