Programmer's Python - Objects Become Classes
Written by Mike James   
Monday, 09 November 2020
Article Index
Programmer's Python - Objects Become Classes
Instance Attributes

For example:


Now myMethod is a method object with a number of attributes that reflects its role as a wrapper that converts a function into a method:


  • __self__ is the instance i.e. myObject in this case

  • __func__ is the unmodified function object i.e. myClass.myFunc in this case

  • __doc__ is the docstring of the function i.e. myMethod.__doc__= myClass.myFunc.__doc__

  • __name__ is the functions name i.e. myMethod.__name__= myFunc.__name__

  • __module__ is the module that the function is in i.e. myMethod.__module__ = myObject.myFunc.__module__


Of course, when you call myMethod:


it calls:

myMethod.__func__(myMethod.__self__, *args, **kwargs)

and this is how functions become methods in Python.

You can think of this as in the illustration below:


Included in the chapter but not in this extract:

  • Bind Early, Bind Often
  • Class Binds
  • Other Bindings
  • The Initializer __init__
  • Multiple Constructors
  • Class for Methods, Instance for Data



  • Unlike class in other languages, in Python a class is an object.

  • A class object is a callable which means it can be called like a function.

  • When called as a function a class returns an instance.

  • At its simplest an instance has no attributes. Any attributes you try to access on the instance are resolved by attributes on the class object.

  • If you assign to an attribute then it is created as an instance attribute.

  • Function attributes defined on the class object are converted into method objects when accessed via an instance.

  • Methods call the function object using self, the instance, as the first parameter. What this means is that you write function attributes with an explicit self parameter but when you call a method you do not specify self.

  • Methods are bound to their instance when first accessed; this is a form of early binding.

  • In addition to instance methods you can also create static and class methods.

  • The initalizer __init__ can be used to add attributes to an instance and to initialize them.

  • You can create multiple constructors using class methods.

  • In Python it makes sense to place all methods in the class body and all data attributes in the __init__. This allows methods to be shared by all instances and data to be unique to each instance.


Programmer's Python
Everything is an Object

Is now available as a print book: Amazon



  1. Hello Python World
  2. Variables, Objects and Attributes
  3. The Function Object
      Extract - Function Objects
  4. Scope, Lifetime and Closure
      Extract - Local and Global
      Extract - Closure ***NEW
  5. Advanced Functions
      Extract - Parameters
      Extract - Decorators 
  6. Class Methods and Constructors
      Extract -  Objects Become Classes
  7. Inside Class
  8. Metaclass
  9. Advanced Attributes
      Extract - Properties
  10. Custom Attribute Access
      Extract -  Custom Attributes
      Extract -  Default Methods
  11. Single Inheritance
  12. Multiple Inheritance
  13. Class and Type
      Extract - Class & Type
  14. Type Annotation
      Extract - Type Annotation 
  15. More Magic - Operator Overloading


Advanced Attributes



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





or email your comment to:

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.






Last Updated ( Monday, 09 November 2020 )