Code 14: Python Classes Basics 101

Python Classes, Instance Objects, Data Attributes and Method Objects
coding
python
Author

Tony Phung

Published

October 23, 2024

1. Class Objects

Class objects support two kinds of operations:

  • attribute references
    • Code: ClassObjectName.attribute_name
  • instantiation
    • Code: new_object_instance = ClassObjectName()
class MyClass:
    """A simple example class"""
    i = 12345

    def f(self):
        return 'hello world'

def temp_fn():
    pass

instance_object = MyClass()

2. Classes Objects vs Object Instances Influence on Class Type

An objects class or type dependings on the context in which its created.

  • For instance (pun intended), an object’s class can be different depending whether declared class attribute versus object attribute

2.1 function class vs method class:

A function becomes a method when the function is accessed via an object instance (see examples)

2.2 function and integer class:

These two classes remain as is when declared within a Class. - Wheter standalone or - A Class attribute (e.g. ClassName.function).

  • integer remains as is as Object Instance attribute too.

2.3 Class and Instance Attributes Examples

delim = ": "

line0="type(MyClass.i)" + delim
line0val=type(MyClass.i)

line1="type(MyClass.f)" + delim
line1val=type(MyClass.f)

line2="type(temp_fn)" + delim
line2val=type(temp_fn)

line3="type(instance_object.f)" + delim
line3val=type(instance_object.f)

line4="type(instance_object.i)" + delim
line4val=type(instance_object.i)

print(f"{line0:<25}{str(line0val)}") # experimenting new way to print()
print(f"{line1:<25}{str(line1val)}")
print(f"{line2:<25}{str(line2val)}")
print(f"{line3:<25}{str(line3val)}")
print(f"{line4:<25}{str(line4val)}")
type(MyClass.i):         <class 'int'>
type(MyClass.f):         <class 'function'>
type(temp_fn):           <class 'function'>
type(instance_object.f): <class 'method'>
type(instance_object.i): <class 'int'>

3. Instance Objects

Instance Objects have two types of attributes (or instance attribute reference):

  • Data Attributes
  • Method Objects

3.1 Data Attributes

  • Need not be declared
  • data members in C++
  • spring into existence when first assigned

3.1.1 Example

Below counter is data_attribute of the object instance_object (which is of type MyClass).

counters value 16 is printed, then deleted without leaving a trace:

print(type(instance_object))  

instance_object.counter = 1

print(type(instance_object.counter))  

while instance_object.counter < 10:
    instance_object.counter = instance_object.counter * 2
print(instance_object.counter)
del instance_object.counter

print(type(instance_object.counter))  
<class '__main__.MyClass'>
<class 'int'>
16
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[3], line 12
      9 print(instance_object.counter)
     10 del instance_object.counter
---> 12 print(type(instance_object.counter))  

AttributeError: 'MyClass' object has no attribute 'counter'

4. Method Objects

  • Functions that belong to an object.
  • Are called method objects, not function objects.

If x is an instance object, then x.f is a method object.

Method objects can be:

  • [4.1] assigned first: xf = x.f
  • [4.2] and then called: xf()

4.1 Assign a Method Object

# assign method object
xf = instance_object.f
xf
<bound method MyClass.f of <__main__.MyClass object at 0x7fc46c36b8b0>>

4.2 Call the Method Oject

xf()
'hello world'