From CS 61A Wiki
Jump to: navigation, search

An object can be seen as a structured collection of stored data and methods. An object belonging to a specific class or data type is an instance of that class. A class is just an overarching representation of its instance objects, an abstract category to which the concrete objects can be classified.


Main article: Attribute

An attribute is a piece of data associated with the object. For instance, a Car object can have a gray color, in which case, color is an attribute of that object with the value gray.

There are many caterogies of attributes. One special category is methods, where the associated piece of data is a function. For example, all Python strings have a method upper, which when invoked with the object, will return a new string with all characters from the object uppercased.


self is a conventionally used term to refer to the current object so that attributes and methods can be accessed. Instance methods use self as a parameter to reference the instance object that the method is being invoked on. In the following example, Car.start(bmw) and bmw.start() are equivalent. In Car.start(bmw), the Car class calls the start method, and the bmw object is the argument passed to self parameter. In bmw.start(), the self parameter is not shown but refers to the bmw object that calls the start function.

>>> class Car():
        count = 0
        def __init__(self):
            print('car ' + str(Car.count))
        def start(self):
            print('starting engine')
>>> bmw = Car()
car 1
>>> Car.start(bmw)
starting engine
>>> bmw.start()
starting engine


In Python, everything is an object. Individual functions, integers, strings, and data structures are all instance objects of their respective classes.

>>> type(1)
<class 'int'>
>>> type('hello world')
<class 'str'>
>>> type([1,2,3,4])
<class 'list'>

All objects in Python have local state that can be managed through bound methods, which are functions invoked on objects as described above.


In a Python functional implementation of Object-oriented programming, an object can be represented as a dispatch dictionary within a local frame[1]. Within the dispatch dictionary, get and set are keys that lead to the functions that implement those attribute operations. The instance's attributes exist as a dictionary in the local frame but cannot be accessed directly: they must be accessed instead from the get and set operations.

>>> # From Section 2.6.1 of Composing Programs by John DeNero (see citation above)
>>> def make_instance(cls):
        """Return a new object instance, which is a dispatch dictionary."""
        def get_value(name):
            if name in attributes:
                return attributes[name]
                value = cls['get'](name)
                return bind_method(value, instance)
        def set_value(name, value):
            attributes[name] = value
        attributes = {}
        instance = {'get': get_value, 'set': set_value}
        return instance


  1. http://composingprograms.com/pages/26-implementing-classes-and-objects.html#instances