{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "from __future__ import print_function" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "# Classes" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Classes are the fundamental concept for object oriented programming. A class defines a data type with both data and functions that can operate on the data. An object is an instance of a class. Each object will have its own namespace (separate from other instances of the class and other functions, etc. in your program).\n", "\n", "We use the dot operator, `.`, to access members of the class (data or functions). We've already been doing this a lot, strings, ints, lists, ... are all objects in python." ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "simplest example: just a container (like a struct in C)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "class Container(object):\n", " pass\n", " \n", "a = Container()\n", "a.x = 1\n", "a.y = 2\n", "a.z = 3\n", "\n", "b = Container()\n", "b.xyz = 1\n", "b.uvw = 2\n", "\n", "print(a.x, a.y, a.z)\n", "print(b.xyz, b.uvw)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "notice that you don't have to declare what variables are members of the class ahead of time (although, usually that's good practice).\n", "\n", "Here, we give the class name an argument, `object`. This is an example of inheritance. For a general class, we inherit from the base python `object` class." ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "## More useful class" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Here's a class that holds some student info" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "class Student(object):\n", " def __init__(self, name, grade=None):\n", " self.name = name\n", " self.grade = grade" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Let's create a bunch of them, stored in a list" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "students = []\n", "students.append(Student(\"fry\", \"F-\"))\n", "students.append(Student(\"leela\", \"A\"))\n", "students.append(Student(\"zoidberg\", \"F\"))\n", "students.append(Student(\"hubert\", \"C+\"))\n", "students.append(Student(\"bender\", \"B\"))\n", "students.append(Student(\"calculon\", \"C\"))\n", "students.append(Student(\"amy\", \"A\"))\n", "students.append(Student(\"hermes\", \"A\"))\n", "students.append(Student(\"scruffy\", \"D\"))\n", "students.append(Student(\"flexo\", \"F\"))\n", "students.append(Student(\"morbo\", \"D\"))\n", "students.append(Student(\"hypnotoad\", \"A+\"))\n", "students.append(Student(\"zapp\", \"Q\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "