Page 3 of 3
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:
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.
Everything is an Object
Is now available as a print book: Amazon
- Hello Python World
- Variables, Objects and Attributes
- The Function Object
Extract - Function Objects
- Scope, Lifetime and Closure
Extract - Local and Global
Extract - Closure ***NEW
- Advanced Functions
Extract - Parameters
Extract - Decorators
- Class Methods and Constructors
Extract - Objects Become Classes
- Inside Class
- Advanced Attributes
Extract - Properties
- Custom Attribute Access
Extract - Custom Attributes
Extract - Default Methods
- Single Inheritance
- Multiple Inheritance
- Class and Type
Extract - Class & Type
- Type Annotation
Extract - Type Annotation
- More Magic - Operator Overloading
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