class MyClass:
"""A simple example class"""
= 12345
i
def f(self):
return 'hello world'
def temp_fn():
pass
= MyClass() instance_object
1. Class Objects
Class objects support two kinds of operations:
- attribute references
- Code:
ClassObjectName.attribute_name
- Code:
- instantiation
- Code:
new_object_instance = ClassObjectName()
- Code:
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
="type(MyClass.i)" + delim
line0=type(MyClass.i)
line0val
="type(MyClass.f)" + delim
line1=type(MyClass.f)
line1val
="type(temp_fn)" + delim
line2=type(temp_fn)
line2val
="type(instance_object.f)" + delim
line3=type(instance_object.f)
line3val
="type(instance_object.i)" + delim
line4=type(instance_object.i)
line4val
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))
= 1
instance_object.counter
print(type(instance_object.counter))
while instance_object.counter < 10:
= instance_object.counter * 2
instance_object.counter 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
= instance_object.f
xf xf
<bound method MyClass.f of <__main__.MyClass object at 0x7fc46c36b8b0>>
4.2 Call the Method Oject
xf()
'hello world'