Python: Defining Your Own Classes and Objects, Study notes of Linguistics

An introduction to object-oriented programming in python, focusing on defining and instantiating custom classes with instance variables and methods. Topics include creating a simple mypoint class, initializing variables, and using special method __init__.

Typology: Study notes

Pre 2010

Uploaded on 08/27/2009

koofers-user-zuy
koofers-user-zuy 🇺🇸

5

(1)

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LIN 392, Spring 2009
Working with Corpora
Katrin Erk
Object orientation in Python:
Bake your own data types
Defining your own classes
in Python
We have talked about Object Orientation: Objects have
methods associated with them according to their data type.
For example, all string objects have a method startswith()
that tests if a string starts with a given prefix.
You can define your own data types in Python, called classes,
and then you can make objects of your self-defined classes.
Self-defined classes can have associated
!!variables
!!and methods
So you can use them as composite data structures with
associated methods.
pf3
pf4
pf5

Partial preview of the text

Download Python: Defining Your Own Classes and Objects and more Study notes Linguistics in PDF only on Docsity!

LIN 392, Spring 2009

Working with Corpora

Katrin Erk

Object orientation in Python:

Bake your own data types

Defining your own classes

in Python

We have talked about Object Orientation: Objects have

methods associated with them according to their data type.

For example, all string objects have a method startswith()

that tests if a string starts with a given prefix.

You can define your own data types in Python, called classes,

and then you can make objects of your self-defined classes.

Self-defined classes can have associated

!! variables

!! and methods

So you can use them as composite data structures with

associated methods.

A first example of a new class: With variables only, no methods Making a new class: class MyPoint: ”””This class represents a 2-dimensional point””” x = None # integer: x-axis value y = None # integer: y-axis value This defines a constructor function MyPoint() that you can use to make an object of the new class:

myvar = Point() myvar.x = 0 myvar.x 0 Once myvar is a variable of type MyPoint, it has associated instance variables x and y that you can read and write using the dot notation. Making your own types: classes Different instances of the same data type can hold different values in their instance variables. a = MyPoint() a.x = 0 b = MyPoint() b.x = 2 a.x 0 b.x 2

Defining a class with methods

So far, we have only defined classes with associated instance

variables. Now we add methods. The first is a very simple

method that prints out the coordinates of our point:

class MyPoint: x = None y = None def printme(self): print “x:”, self.x, “y:”, self.y Note that the method definition looks (almost) like the function definitions you have seen before, but it is within the block of “class MyPoint”.

Defining a class with methods

class MyPoint: x = None y = None def printme(self): print “x:”, self.x, “y:”, self.y The parameter ‘self’ of the MyPoint method printme is the object to which the method belongs. So, self.x is the x instance variable of the object to which the printme method belongs.

Defining a class with methods

class MyPoint: x = None y = None def printme(self): print “x:”, self.x, “y:”, self.y When you call the ‘printme’ method, you don’t need to provide the ‘self’ parameter. Python does that for you:

a = Point() a.x = 3 a.y = 5 a.printme() x: 3 y: 5

Defining a class with methods:

initialization

There is a special method init. If you define it, it is called when you make a new object of the given class. class MyPoint: x = None y = None def init(self, new_x, new_y): self.x = new_x self.y = new_y

a = MyPoint(3, 5) a.x 3