Programmer's Python - Inside Class
Written by Mike James   
Monday, 10 December 2018
Article Index
Programmer's Python - Inside Class
Uses Of Custom Classes

Singleton

Final version in book

Customizing Immutable Data Objects

Final version in book

How Instances Become Functions

The final question we have to answer is how is it that we can call a class as if it was a function?

The answer is that there is a magic __call__ method and any object which has it defined can be called like a function – it is a callable.

This is the basic idea but there is a subtle extra condition. The __call__ has to be defined on the object’s class object. The Python documentation says that magic methods aren’t guaranteed to work if they are defined on the object rather than its class. This is the case for __call__. If you want an object to be callable you have to define __call__ on its class.

For example:

class MyClass:
     def __call__(self,*args, **kwargs):
        print("class call")

If you now create an instance it can be called like a function:

myObject=MyClass()
myObject()

and you will see “class call” printed.

Being able to create an instance that is a function is very useful but notice that as it is the class __call__ method that is actually called, all of the instances of a class are restricted to use the same function. This is usually a reasonable way to organize things. In general adding methods of any kind to an instance is not best practice. To modify the way an instance works the best option is to create a subclass i.e. use inheritance and override the methods you want to change - see Chapter 11.

See the next chapter to discover how class objects become callable.

Summary

 

  • __new__ is automatically called to create the instance.

  • __init__ is called after __new__ to add instance attributes to the new instance.

  • __new__ generally works by calling the superclass __new__ to create an instance but you can use any mechanism that creates an object.

  • As long as the object is of the same type as the class or is a subtype the __init__ method is called.

  • One standard example of how __new__ can be used is to create a singleton i.e. a class that can only be instantiated once.

  • Another use of __new__ is to customize immutable data objects.

  • Adding a custom __call__ method can be used to convert any object into a callable – this is how class is converted to a callable.

 

Programmer's Python
Everything is an Object
Second Edition

Is now available as a print book: Amazon

pythonObject2e360

Contents

  1. Get Ready For The Python Difference
  2. Variables, Objects and Attributes
  3. The Function Object
  4. Scope, Lifetime and Closure
      Extract 1: Local and Global ***NEW!
  5. Advanced Functions
  6. Decorators
  7. Class, Methods and Constructors
      Extract 1: Objects Become Classes 
  8. Inside Class
  9. Meeting Metaclasses
  10. Advanced Attributes
  11. Custom Attribute Access
  12. Single Inheritance
  13. Multiple Inheritance
  14. Class and Type
  15. Type Annotation
  16. Operator Overloading
  17. Python In Visual Studio Code

 Extracts from the first edition

<ASIN:1871962749>

<ASIN:1871962595>

<ASIN:1871962765>

Related Articles

Creating The Python UI With Tkinter

Creating The Python UI With Tkinter - The Canvas Widget

The Python Dictionary

Arrays in Python

Advanced Python Arrays - Introducing NumPy


espbook

 

Comments




or email your comment to: comments@i-programmer.info

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner

<ASIN:1871962587>



Last Updated ( Monday, 10 December 2018 )