DelphiDelphiVCLPythonPython GUIWindows

More On Built-In Function Objects Inside DelphiVCL Library (Part 3)

featuredimage2 4

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. Previously, we already discussed about  __doc__, __file__, __loader__, __name__, __package__,__spec__, __class__, __contains__, __delattr__, __dir__, __eq__, __format__, __ge__, __getattribute__, __getitem__, __gt__, __hash__, __init__, __init_subclass__, and __iter__.

Here are more built-in function objects inside the DelphiVCL library and their descriptions:

__le__: object.__le__(self, other)

__le__ 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.

 

__len__: object.__len__(self)

Called to implement the built-in function len(). Should return the length of the object, an integer >= 0. Also, an object that doesn’t define a __bool__() method and whose __len__() method returns zero is considered to be false in a Boolean context.

CPython implementation detail: In CPython, the length is required to be at most sys.maxsize. If the length is larger than sys.maxsize some features (such as len()) may raise OverflowError. To prevent raising OverflowError by truth value testing, an object must define a __bool__() method.

Run this __len__() example in your PyScripter IDE:

Here are the outputs:

1 8278321

 

__lt__: object.__lt__(self, other)

__lt__ 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.

 

__ne__: object.__ne__(self, other)

__ne__ 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).

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.

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.

 

__new__: object.__new__(cls[, …])

Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of __new__() should be the new object instance (usually an instance of cls).

Typical implementations create a new instance of the class by invoking the superclass’s __new__() method using super().__new__(cls[, …]) with appropriate arguments and then modifying the newly-created instance as necessary before returning it.

If __new__() is invoked during object construction and it returns an instance or subclass of cls, then the new instance’s __init__() method will be invoked like __init__(self[, …]), where self is the new instance and the remaining arguments are the same as were passed to the object constructor.

If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.

__new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses to customize class creation.

 

__reduce__: object.__reduce__()

The interface is currently defined as follows. The __reduce__() method takes no argument and shall return either a string or preferably a tuple (the returned object is often referred to as the “reduce value”).

If a string is returned, the string should be interpreted as the name of a global variable. It should be the object’s local name relative to its module; the pickle module searches the module namespace to determine the object’s module. This behavior is typically useful for singletons.

When a tuple is returned, it must be between two and six items long. Optional items can either be omitted, or None can be provided as their value. The semantics of each item are in order:

  • A callable object will be called to create the initial version of the object.
  • A tuple of arguments for the callable object. An empty tuple must be given if the callable does not accept any argument.
  • Optionally, the object’s state, which will be passed to the object’s __setstate__() method as previously described. If the object has no such method then, the value must be a dictionary and it will be added to the object’s __dict__ attribute.
  • Optionally, an iterator (and not a sequence) yielding successive items. These items will be appended to the object either using obj.append(item) or, in batch, using obj.extend(list_of_items). This is primarily used for list subclasses, but may be used by other classes as long as they have append() and extend() methods with the appropriate signature. (Whether append() or extend() is used depends on which pickle protocol version is used as well as the number of items to append, so both must be supported.)
  • Optionally, an iterator (not a sequence) yielding successive key-value pairs. These items will be stored to the object using obj[key] = value. This is primarily used for dictionary subclasses, but may be used by other classes as long as they implement __setitem__().
  • Optionally, a callable with a (obj, state) signature. This callable allows the user to programmatically control the state-updating behavior of a specific object, instead of using obj’s static __setstate__() method. If not None, this callable will have priority over obj’s __setstate__().

New in version 3.8: The optional sixth tuple item, (obj, state), was added.

 

__reduce_ex__: object.__reduce_ex__(protocol)

Alternatively, a __reduce_ex__() method may be defined. The only difference is this method should take a single integer argument, the protocol version. When defined, the pickle will prefer it over the __reduce__() method. In addition, __reduce__() automatically becomes a synonym for the extended version. The main use for this method is to provide backward-compatible reduce values for older Python releases.

 

Check out DelphiVCL which easily allows you to build GUIs for Windows using Python.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Weather App With The Python Delphi Ecosystem and Weatherstack API

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Music Player With The Python Delphi Ecosystem

Leave a Reply

Your email address will not be published. Required fields are marked *