0% found this document useful (0 votes)
2 views5 pages

Python Notes Set 5

The document covers advanced Python function concepts, including the use of *args and **kwargs for variable arguments, the LEGB rule for scope and namespaces, and context managers for resource management. It also discusses multithreading for I/O-bound tasks and lists useful built-in functions for object manipulation. Overall, it provides essential programming techniques and best practices in Python.

Uploaded by

fake786king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Python Notes Set 5

The document covers advanced Python function concepts, including the use of *args and **kwargs for variable arguments, the LEGB rule for scope and namespaces, and context managers for resource management. It also discusses multithreading for I/O-bound tasks and lists useful built-in functions for object manipulation. Overall, it provides essential programming techniques and best practices in Python.

Uploaded by

fake786king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Page 1: Advanced Function Concepts

*args and **kwargs:

def example(*args, **kwargs):

for arg in args:

print(arg)

for key, value in kwargs.items():

print(f"{key}: {value}")

Use when number of arguments is unknown.


Page 2: Scope and Namespaces

LEGB Rule:

- Local

- Enclosing

- Global

- Built-in

Use global to modify global variables inside functions.


Page 3: Context Managers

with open('file.txt', 'r') as f:

contents = f.read()

Custom context managers using class or contextlib.

from contextlib import contextmanager

@contextmanager

def managed_resource():

print("Setup")

yield

print("Cleanup")
Page 4: Multithreading

import threading

def worker():

print("Working")

t = threading.Thread(target=worker)

t.start()

t.join()

Good for I/O-bound tasks, not CPU-bound (use multiprocessing for that).
Page 5: Useful Built-in Functions

- len(), type(), range(), zip(), enumerate()

- any(), all(), sorted(), reversed()

- isinstance(), id(), dir(), vars()

Help with inspecting and manipulating objects.

You might also like