Skip to content

Commit 6ec55c7

Browse files
committed
Adding some notes about the ctypes module and GIL
1 parent 4618852 commit 6ec55c7

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

advanced-oop.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ Decorators are a way to modify or enhance the behaviour of functions or methods.
244244

245245
Decorators allow you to create a read-only attribute
246246

247+
The `property` function (`@property` decorator) returns a descriptor object.
248+
247249
```python
248250
def require_admin(func):
249251
def wrapper(user, *args, **kwargs):
@@ -513,6 +515,7 @@ Metaclasses are classes of classes.
513515
* can replace or modify attributes of classes
514516
* have the ability to control class creation by modifying or replacing it
515517
* has similarites to superclasses, but you want to use a metaclass when modifying or enforcing structure at the **class definition level** whereas a superclass is used for sharing logic or data across instances
518+
* the `__new__` method in a metaclass is a factory that is responsible for creating the actual class object. It runs before `__init__`.
516519
* they are utilised by frameworks such as [Django](https://www.djangoproject.com/) and [Flask](https://flask.palletsprojects.com/en/stable/)
517520
```python
518521
class ScottishMeta(type):
@@ -565,6 +568,7 @@ Sets:
565568
* yields values lazily, meaning it produces each value only when requested and resumes execution from where it left off after each `yield`
566569
* When the generator is exhausted, it raises a `StopIteration` exception to signal that there are no more values to `yield`
567570
* If a generator function executes a `return` statement, it raises a `StopIteration` exception with the value specified in the `return` statement as its argument
571+
* `send(value)` - resumes the generator and sends a value that replaces the result of the current `yield` expression
568572
```python
569573
def process_files():
570574
print("Opening file system")

coding-conventions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ microsoft_azure = 3
4949

5050
The restricted line length originates from the early days of computing when terminals were limited to 80 characters per line. This convention is still followed in Python to ensure code readability across different editors and environments.
5151

52-
### Trailing commas
52+
### [Trailing commas](https://peps.python.org/pep-0008/#when-to-use-trailing-commas)
5353
Trailing commas are mandatory when making a tuple of one element, even although they appear redundant.
5454
```python
5555
towns = ('Mauchline',)
@@ -159,7 +159,7 @@ def get_user(user_id: UserId):
159159
To make a class usable with a `with` statement, you need to implement the `__enter__()` and `__exit__()` methods.
160160

161161
* `__enter__()` - called when the block is entered
162-
* `__exit__()` - called when the block is exited. Always called even if an exception occurs inside the `with` block. This allows context managers to clean up resources.
162+
* `__exit__()` - called when the block is exited. Always called even if an exception occurs inside the `with` block. This allows context managers to clean up resources. When an exception is raised inside the `with` block, Python automatically calls `__exit__()`.
163163

164164
```python
165165
class APIConnection:

network-programming.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ print(json_output)
7474
## socket
7575
The `socket` module provides access to the BSD socket interface. It is used to create network connections and communicate over the network.
7676

77+
It has three parts:
78+
1. Protocol
79+
2. Local address
80+
3. Local port
81+
7782
`socket.accept()` - allows a server socket to accept requests from a client socket from another host.
7883
```python
7984
import socket
@@ -125,6 +130,10 @@ service = socket.getservbyport(53, 'udp')
125130
print(f"Service on UDP port 53: {service}")
126131
# domain
127132
```
133+
`SOCK_STREAM` - provides a reliable, connection-oriented byte stream. Used for TCP connections.
134+
135+
`SOCK_DGRAM` - provides a connectionless, unreliable **d**ata**gram** service. Used for UDP connections.
136+
128137
Clients - request services from servers. They initiate the connection to the server and send requests for data or services.
129138

130139
Servers - provide services to clients. They listen for incoming connections and respond to client requests.
@@ -241,4 +250,7 @@ The `multiprocessing` module bypasses the Global Interpreter Lock (GIL) by using
241250
`queue.Queue` is thread-safe, meaning that multiple threads can safely add and remove items from the queue without causing data corruption or inconsistencies. Specifically designed for thread-safe communication. Safe to use across processes. However, the order in which items arrive is not guaranteed because the processes run in parallel.
242251

243252
#### Global Interpreter Lock (GIL)
244-
The GIL is a mutex/lock used by CPython (the standard implementation of Python) to protect access to Python objects, preventing multiple threads from executing Python bytecodes at once. This means that even in a multithreaded program, only one thread can execute Python code at a time.
253+
The GIL is a mutex/lock used by CPython (the standard implementation of Python) to protect access to Python objects, simplify memory management, preventing multiple threads from executing Python bytecodes at once. This means that even in a multithreaded program, only one thread can execute Python code at a time.
254+
255+
### ctypes
256+
`ctypes` serves as a foreign function library, enabling Python to wrap and call functions from compiled C libraries without writing C extension code.

0 commit comments

Comments
 (0)