How Objects Are Represented

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Objectives:

  • Learn more about how objects are represented.
  • Learn how attribute assignment and lookup works.
  • Better understand the role of a class definition

Files Modified: None


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") subgraph Lab Skills python/lists -.-> lab-132499{{"`How Objects Are Represented`"}} python/tuples -.-> lab-132499{{"`How Objects Are Represented`"}} python/function_definition -.-> lab-132499{{"`How Objects Are Represented`"}} python/standard_libraries -.-> lab-132499{{"`How Objects Are Represented`"}} python/classes_objects -.-> lab-132499{{"`How Objects Are Represented`"}} python/constructor -.-> lab-132499{{"`How Objects Are Represented`"}} python/polymorphism -.-> lab-132499{{"`How Objects Are Represented`"}} python/encapsulation -.-> lab-132499{{"`How Objects Are Represented`"}} python/python_shell -.-> lab-132499{{"`How Objects Are Represented`"}} end

Preparation

Start this lab, by going back to a simple version of the Stock class you created. At the interactive prompt, define a new class called SimpleStock that looks like this:

>>> class SimpleStock:
        def __init__(self, name, shares, price):
            self.name = name
            self.shares = shares
            self.price = price
        def cost(self):
            return self.shares * self.price

>>>

Once you have defined this class, create a few instances.

>>> goog = SimpleStock('GOOG',100,490.10)
>>> ibm  = SimpleStock('IBM',50, 91.23)
>>>

Representation of Instances

At the interactive shell, inspect the underlying dictionaries of the two instances you created:

>>> goog.__dict__
... look at the output ...
>>> ibm.__dict__
... look at the output ...
>>>

Modification of Instance Data

Try setting a new attribute on one of the above instances:

>>> goog.date = "6/11/2007"
>>> goog.__dict__
... look at output ...
>>> ibm.__dict__
... look at output ...
>>>

In the above output, you'll notice that the goog instance has a attribute date whereas the ibm instance does not. It is important to note that Python really doesn't place any restrictions on attributes. For example, the attributes of an instance are not limited to those set up in the __init__() method.

Instead of setting an attribute, try placing a new value directly into the __dict__ object:

>>> goog.__dict__['time'] = '9:45am'
>>> goog.time
'9:45am'
>>>

Here, you really notice the fact that an instance is a layer on top of a dictionary.

The role of classes

The definitions that make up a class definition are shared by all instances of that class. Notice, that all instances have a link back to their associated class:

>>> goog.__class__
... look at output ...
>>> ibm.__class__
... look at output ...
>>>

Try calling a method on the instances:

>>> goog.cost()
49010.0
>>> ibm.cost()
4561.5
>>>

Notice that the name 'cost' is not defined in either goog.__dict__ or ibm.__dict__. Instead, it is being supplied by the class dictionary. Try this:

>>> SimpleStock.__dict__['cost']
... look at output ...
>>>

Try calling the cost() method directly through the dictionary:

>>> SimpleStock.__dict__['cost'](goog)
49010.00
>>> SimpleStock.__dict__['cost'](ibm)
4561.5
>>>

Notice how you are calling the function defined in the class definition and how the self argument gets the instance.

If you add a new value to the class, it becomes a class variable that's visible to all instances. Try it:

>>> SimpleStock.spam = 42
>>> ibm.spam
42
>>> goog.spam
42
>>>

Observe that spam is not part of the instance dictionary.

>>> ibm.__dict__
... look at the output ...
>>>

Instead, it's part of the class dictionary:

>>> SimpleStock.__dict__['spam']
42
>>>

Essentially this is all a class really is--it's a collection of values shared by instances.

Summary

Congratulations! You have completed the How Objects Are Represented lab. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like