You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: advanced-oop.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -244,6 +244,8 @@ Decorators are a way to modify or enhance the behaviour of functions or methods.
244
244
245
245
Decorators allow you to create a read-only attribute
246
246
247
+
The `property` function (`@property` decorator) returns a descriptor object.
248
+
247
249
```python
248
250
defrequire_admin(func):
249
251
defwrapper(user, *args, **kwargs):
@@ -513,6 +515,7 @@ Metaclasses are classes of classes.
513
515
* can replace or modify attributes of classes
514
516
* have the ability to control class creation by modifying or replacing it
515
517
* 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__`.
516
519
* they are utilised by frameworks such as [Django](https://www.djangoproject.com/) and [Flask](https://flask.palletsprojects.com/en/stable/)
517
520
```python
518
521
classScottishMeta(type):
@@ -565,6 +568,7 @@ Sets:
565
568
* yields values lazily, meaning it produces each value only when requested and resumes execution from where it left off after each `yield`
566
569
* When the generator is exhausted, it raises a `StopIteration` exception to signal that there are no more values to `yield`
567
570
* 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
Copy file name to clipboardExpand all lines: coding-conventions.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,7 @@ microsoft_azure = 3
49
49
50
50
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.
To make a class usable with a `with` statement, you need to implement the `__enter__()` and `__exit__()` methods.
160
160
161
161
*`__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__()`.
Copy file name to clipboardExpand all lines: network-programming.md
+13-1Lines changed: 13 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -74,6 +74,11 @@ print(json_output)
74
74
## socket
75
75
The `socket` module provides access to the BSD socket interface. It is used to create network connections and communicate over the network.
76
76
77
+
It has three parts:
78
+
1. Protocol
79
+
2. Local address
80
+
3. Local port
81
+
77
82
`socket.accept()` - allows a server socket to accept requests from a client socket from another host.
78
83
```python
79
84
import socket
@@ -125,6 +130,10 @@ service = socket.getservbyport(53, 'udp')
125
130
print(f"Service on UDP port 53: {service}")
126
131
# domain
127
132
```
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
+
128
137
Clients - request services from servers. They initiate the connection to the server and send requests for data or services.
129
138
130
139
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
241
250
`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.
242
251
243
252
#### 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