Tuesday, February 23, 2016

Python Jargon Reference


ABC : 
Abstract Base Class. Same is Abstract class in C++. It can be used for inheriting the subclass.

accessors : 
Normally getter and setter fucntions for a given attributes. Some refer accessors as getters only and use mutators for setters.

aliasing :
Assigning more than one name to an object. Example a = [1,2,3,4] and b=a. In this case both a and b points to the same object.

binary sequence: 
Generally refers to sequence of binary bytes. The built-in binary sequence types are byte and bytes. Example
>>> key = bytes([0x13, 0x00, 0x00, 0x00, 0x08, 0x00])
>>> print(key)
b'\x13\x00\x00\x00\x08\x00'
>>> key = bytearray([0x13, 0x00, 0x00, 0x00, 0x08, 0x00])
>>> key
bytearray(b'\x13\x00\x00\x00\x08\x00')


callable object:
An object that can be invoked with the call operator (), to return a result or to perform some action. There are seven flavors of callable objects in Python: user-defined functions, built-in functions, built-in methods, instance methods, generator functions, classes, and instances of classes that implement the __call__ special method.

cheese shop:
Original name of the Python Package Index (PyPI), https://cheeseshop.python.org

collection:
Generic term for data structures made of items that can be accessed individually. Some collections can contain objects of arbitrary types (see container) and others only objects of a single atomic type (see flat sequence).
list and bytes are both collections, but list is a container, and bytes is a flat sequence.

constructor:
Informally, the __init__ instance method of a class is called its constructor, because its semantics is similar to that of a Java constructor. However, a fitting name for __init__ is initializer, as it does not actually build the instance, but receives it as its self argument. The constructor term better describes the __new__ class method, which Python calls before __init__, and is responsible for actually creating an instance and returning it. See initializer.

container:
An object that holds references to other objects. Example : List of ints, int is an object by itself.

duck typing:
A form of polymorphism where functions operate on any object that implements the appropriate methods, regardless of their classes or explicit interface declarations.

dunder:
Shortcut to pronounce the names of special methods and attributes that are written with leading and trailing double-underscores (i.e., __len__ is read as “dunder len”).

eager:
An iterable object that builds all its items at once. In Python, a list comprehension is eager.

first-class function:
Any function that is a first-class object in the language (i.e., can be created at runtime, assigned to variables, passed as an argument, and returned as the result of another function). Python functions are first-class functions.

generator function:
A function that has the yield keyword in its body. When invoked, a generator function returns a generator. Example: Function to generate fibnocci numbers.

hashable:
An object is hashable if it has both __hash__ and __eq__ methods, with the constraints that the hash value must never change and if a == b then hash(a) == hash(b) must also be True. Most immutable built-in types are hashable,
but a tuple is only hashable if every one of its items is also hashable.
>>> a=10
>>> b=a
>>> a==b
True
>>> hash(a)==hash(b)
True
>>> b=30
>>> hash(a)==hash(b)
False
>>>
Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.
All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Objects which are instances of user-defined classes are hashable by default; they all compare unequal, and their hash value is their id().

weak reference:
A special kind of object reference that does not increase the referent object reference count. Weak references are created with one of the functions and data structures in the weakref module.

higher-order function:
A function that takes another function as argument, like sorted, map, and filter, or a function that returns a function as result, as Python decorators do.

iterable:
Any object from which the iter built-in function can obtain an iterator. An iterable object works as the source of items in for loops, comprehensions, and tuple unpacking. Objects implementing an __iter__ method returning an
iterator are iterable. Sequences are always iterable; other objects implementing a __getitem__ method may also be iterable.

iterator:
Any object that implements the __next__ no-argument method, which returns the next item in a series, or raises StopIteration when there are no more items. Python iterators also implement the __iter__ method so they are also
iterable. Classic iterators, according to the original design pattern, return items from a collection. A generator is also an iterator, but it’s more flexible. See generator.

metaclass:
A class whose instances are classes. By default, Python classes are instances of type, for example, type(int) is the class type, therefore type is a metaclass. User-defined metaclasses can be created by subclassing type.

refcount:
The reference counter that each CPython object keeps internally in order to determine when it can be destroyed by the garbage collector.

referent:
The object that is the target of a reference. This term is most often used to discuss weak references.

sequence:
Generic name for any iterable data structure with a known size (e.g., len(s)) and allowing item access via 0-based integer indexes (e.g., s[0]). The word sequence has been part of the Python jargon from the start, but only with
Python 2.6 was it formalized as an abstract class in collections.abc.Sequence.

serialization:
Converting an object from its in-memory structure to a binary or text-oriented format for storage or transmission, in a way that allows the future reconstruction of a clone of the object on the same system or on a different one.
The pickle module supports serialization of arbitrary Python objects to a binary format.

singleton:
An object that is the only existing instance of a class—usually not by accident but because the class is designed to prevent creation of more than one instance. There is also a design pattern named Singleton, which is a recipe for coding such classes. The None object is a singleton in Python.

strong reference:
A reference that keeps an object alive in Python. Contrast with weak reference.

view:
Python 3 views are special data structures returned by the dict methods .keys(), .values(), and .items(), providing a dynamic view into the dict keys and values without data duplication, which occurs in Python 2 where those methods
return lists. All dict views are iterable and support the in operator. In addition, if the items referenced by the view are all hashable, then the view also implements the collections.abc.Set interface. This is the case for all views returned by the .keys() method, and for views returned by .items() when the values are also hashable.

No comments: