1234 3.14159 "Hello" [13,5,17,11,13] {"DEL": "Delhi", "MUM": "Mumbai"}
1234
is an instance of an int
"hello"
is an instance of a string
del
or just "forget" about themL[i]
, L[i:j]
len(), min(), max(), del(L[i])
L.append(), L.extend(), L.count(), L.index(), L.insert(), L.pop(), L.remove(), L.reverse(), L.sort()
L=[1,2]
and len(L)
.class
keyword to define a new type
class Coordinate(object): # "class keyword" "name/type Coordinate" "class parent object" # define attributes here
def
, indent code to indicate which statements are a part of the class definition.object
means that a Coordinate
object is
also a Python object and inherits all its attributes (inheritance next lecture)
Coordinate
is a subclass of object
object
is a superclass of Coordinate
class Coordinate(object): # special method to create an instance. __ is double underscore def __init__(self, x, y): # "self" is a parameter to refer to an instance of the class. # "x" and "y" are data to initialize a Coordinate object self.x = x #x is a data attribute for every Coordinate object self.y = y #y is a data attribute for every Coordinate object
c = Coordinate(3, 4) # create a new object of type Coordinate and pass in 3 and 4 to the __init__ method origin = Coordinate(0, 0) print(c.x) # use the dot to access an attribute of instance c print(origin.x) # similarly, access an attribute of instance origin
self
, Python does this automaticallyself
as the name of the first argument of all methodsclass Coordinate(object): def __init__(self, x, y): self.x = x self.y = y def distance(self, other): # self refers to the instance on which this method is called # other is another parameter to the function call x_diff_sq = (self.x - other.x)**2 #dot notation to access data y_diff_sq = (self.y - other.y)**2 return (x_diff_sq + y_diff_sq)**0.5
self
and dot notation, methods behave
just like functions (take params, do operations, return).def distance(self, other): # code here
Using a class:
c = Coordinate(3, 4) zero = Coordinate(0, 0) print(c.distance(zero)) # "c" is an object to call method on # "distance" is the name of the method # "zero" represents parameters not including self; self is implied to be c
c = Coordinate(3, 4) zero = Coordinate(0, 0) print(Coordinate.distance(c, zero)) #Coordinate # "Coordinate" is a name of class # "distance" is the name of the method # "zero" represents parameters including an object to call the method on, representing self
>>> c = Coordinate(3,4) >>> print(c) < __main__.Coordinate object at 0x7fa918510488
__str__
method when used to print
on your class objectCoordinate
object, want to show
>>> print(c) <3,4>
class Coordinate(object): def __init__(self, x, y): self.x = x self.y = y def distance(self, other): x_diff_sq = (self.x - other.x)**2 y_diff_sq = (self.y - other.y)**2 return (x_diff_sq + y_diff_sq)**0.5 def __str__(self): # __str__ is the name of a special method return "<"+str(self.x)+","+str(self.y)+">" #the return value must be a string
>>> c = Coordinate(3,4) >>> print(c) <3,4> #return value of the __str__ method >>> print(type(c)) <class __main__.Coordinate> #type of object c is a class Coordinate
>>> print(Coordinate) <class __main__.Coordinate> #a Coordinate is a class >>> print(type(Coordinate)) <type 'type'> #a Coordinate class is a type of object
isinstance()
to check if an object is a Coordinate
:
>>> print(isinstance(c, Coordinate)) True
+,-,==,<,>,len(),print, and many others
print
, can override these to work with your class__add__(self, other) # self + other __sub__(self, other) # self - other __eq__(self, other) # self == other __lt__(self, other) # self < other __len__(self) # len(self) __str__(self) # print(self) ... and others
Fraction
objects