When running dir() to DelphiVCL library or any DelphiVCL methods and properties in all previous sections, you might find many of Python’s built-in objects and properties. You might also want to read 7 More Built-In Function Objects Inside The DelphiVCL Library which has some other built-in object functions which are related to the ones in this article.
In this post, you’ll learn more about built-in function objects inside the DelphiVCL library and their descriptions. Learning more about built-in function objects will allow you to easily build GUIs with Python Development Tools.
Table of Contents
What is __class__?
__class__: Special attributes __class__ is the instance’s class.
For more fine-grained customization of the module behavior (setting attributes, properties, etc.), one can set the __class__ attribute of a module object to a subclass of types.ModuleType. For example:
1 2 3 4 5 6 7 8 9 10 11 12 |
import sys from types import ModuleType class VerboseModule(ModuleType): def __repr__(self): return f'Verbose {self.__name__}' def __setattr__(self, attr, value): print(f'Setting {attr}...') super().__setattr__(attr, value) sys.modules[__name__].__class__ = VerboseModule |
Since Python version 3.5, __class__ module attribute is now writable.
What does __contains__ do?
Called to implement membership test operators. Should return true if the item is in self, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs.
For objects that don’t define __contains__(), the membership test first tries iteration via __iter__(), then the old sequence iteration protocol via __getitem__(), see this section in the language reference.
What is __delattr__?
Like __setattr__() but for attribute deletion instead of assignment. This should only be implemented if the del obj.name is meaningful for the object.
For certain sensitive attribute deletions, raises an auditing event object.__delattr__ with arguments obj and name.
Attribute assignments and deletions update the instance’s dictionary, never a class’s dictionary. If the class has a __setattr__() or __delattr__() method, this is called instead of updating the instance dictionary directly.
What is __dir__?
Called when dir() is called on the object. A sequence must be returned. dir() converts the returned sequence to a list and sorts it.
Special names __getattr__ and __dir__ can be also used to customize access to module attributes. The __getattr__ function at the module level should accept one argument which is the name of an attribute and return the computed value or raise an AttributeError. If an attribute is not found on a module object through the normal lookup, i.e. object.__getattribute__(), then __getattr__ is searched in the module __dict__ before raising an AttributeError. If found, it is called with the attribute name and the result is returned.
The __dir__ function should accept no arguments, and return a sequence of strings that represents the names accessible on the module. If present, this function overrides the standard dir() search on a module.
What does __eq__ do?
__eq__ is one of the so-called “rich comparison” methods. The correspondence between operator symbols and method names is as follows:
- x<y calls x.__lt__(y),
- x<=y calls x.__le__(y),
- x==y calls x.__eq__(y),
- x!=y calls x.__ne__(y),
- x>y calls x.__gt__(y), and
- x>=y calls x.__ge__(y).
A rich comparison method may return the singleton NotImplemented if it does not implement the operation for a given pair of arguments. By convention, False and True are returned for a successful comparison. However, these methods can return any value, so if the comparison operator is used in a Boolean context (e.g., in the condition of an if statement), Python will call bool() on the value to determine if the result is true or false.
By default, object implements __eq__() by using is, returning NotImplemented in the case of a false comparison: True if x is y else NotImplemented. For __ne__(), by default it delegates to __eq__() and inverts the result unless it is NotImplemented. There are no other implied relationships among the comparison operators or default implementations; for example, the truth of (x<y or x==y) does not imply x<=y. To automatically generate ordering operations from a single root operation, see functools.total_ordering().
What is the purpose of __format__?
Called by the format() built-in function, and by extension, evaluation of formatted string literals and the str.format() method, to produce a “formatted” string representation of an object. The format_spec argument is a string that contains a description of the formatting options desired. The interpretation of the format_spec argument is up to the type implementing __format__(), however, most classes will either delegate formatting to one of the built-in types, or use a similar formatting option syntax.
The return value must be a string object.
Changed in Python version 3.4: The __format__ method of the object itself raises a TypeError if passed any non-empty string.
Started from Python version 3.7, object.__format__(x, ‘’) is now equivalent to str(x) rather than format(str(x), ‘’).
What does __ge__ mean?
__ge__ is one of the so-called “rich comparison” methods. The correspondence between operator symbols and method names is as follows:
- x<y calls x.__lt__(y),
- x<=y calls x.__le__(y),
- x==y calls x.__eq__(y),
- x!=y calls x.__ne__(y),
- x>y calls x.__gt__(y), and
- x>=y calls x.__ge__(y).
There are no swapped-argument versions of these methods (to be used when the left argument does not support the operation but the right argument does); rather, __lt__() and __gt__() are each other’s reflection, __le__() and __ge__() are each other’s reflection, and __eq__() and __ne__() are their own reflection. If the operands are of different types, and the right operand’s type is a direct or indirect subclass of the left operand’s type, the reflected method of the right operand has priority, otherwise, the left operand’s method has priority. Virtual subclassing is not considered.
Check out DelphiVCL which easily allows you to build GUIs for Windows using Python.