You are on page 1of 1

What problem did GIL solve: race conditions in sys.

getrefcount (used to track if


variable can be released from memory; Python does not have garbage collection, at
least in the way it is implemented in e.g. Java) can lead to either memory leaks or
removal of variables that are still used
- multiple locks will have performance issues and possibly cause deadlocks
=> use a single lock on the interpreterer itself
- execution of any Python bytecode requires acquiring the interpreter lock

name mangling - declaring a class variable as __age will prevent direct access from
outside
- still, they can be accessed as follows: sample_instance._SampleClass__value
- Name mangling is particularly useful when you want to ensure that a given
attribute or method won’t get accidentally overwritten. It’s a way to avoid naming
conflicts between classes or subclasses. It’s also useful to prevent subclasses
from overriding methods that have been optimized for better performance.

F expressions represent values;

A Q object (django.db.models.Q) is an object used to encapsulate a collection of


keyword arguments. These keyword arguments are specified as in “Field lookups”
above.
For example, this Q object encapsulates a single LIKE query:
Q(question__startswith="What")

By default, Django adds a Manager with the name objects to every Django model
class. However, if you want to use objects as a field name, or if you want to use a
name other than objects for the Manager, you can rename it on a per-model basis

blank=False - the field is required in forms

author = Author.objects.annotate(age_as_float=Cast("age",
output_field=FloatField())).get()

- Coalesce takes a list of fields and returns the first with a non-null value
Author.objects.create(name="Margaret Smith", goes_by="Maggie")
author = Author.objects.annotate(screen_name=Coalesce("alias", "goes_by",
"name")).get() # Maggie

Django shortcuts - span multiple layers of the MVC


- render() - takes a request, a template, and optionally context dict and content-
type
- redirect() - you can pass a hardcoded URL or a model, in which case it will call
get_absolute_url() on the model
- get_object_or_404()
- get_list_or_404()

You might also like