Beginning Programming in Python
Fall 2019
Agenda
- The basics of Python classes and objects:
- Classes and Objects
- The __init__ constructor method
- Object membership: Dot notation and classes
- Everything is an object in Python!
- Methods: adding functions to class and the self argument
- Object vs. class variables
- Objects Mutability
- Classes and Objects
- Is vs. ==
Objects
- Python supports many different kinds of data
- Each is an object, and every object has:
- a type
- an internal data representation (primitive or composite)
- a set of procedures for interaction with the object
- An object is an instance of a type
- 1234 is an instance of an int
- "hello" is an instance of a string
1234
3.14159
'Hello'
[1, 5, 7, 19]
{'CA':'California', 'TX':'Texas'}
Structured vs. oo programming
Structured Programming:
OO Programming
-
Objects have both data and methods
-
Objects send and receive messages to invoke actions
-
Objects of the same class have the same data elements and methods
-
Key idea in object-oriented programming:
-
The real world can be accurately described as a collection of objects that interact.
-
Object-Oriented Programming
What are objects?
- Objects are a data abstraction that captures...
-
An internal representation
- through data attributes
-
An interface for interacting with objects
- through methods (aka procedures/functions)
- defines behaviors but hides implementation
creating and using your own type
-
Creating the class involves
- defining the class name
- defining class attributes
- for example, someone wrote code to implement a list class
-
Using the class involves
- creating new instances of objects
- doing operations on the instances
- for example, L=[1,2] and len(L)
class definition syntax
class ClassName:
<statement 1>
<statement 2>
.
.
.
A point class
class Point:
"""
Point class represents and
manipulates x,y coords.
"""
pass
p = Point()
type(p)
__init__() or constructor
-
Python Classes can contain functions, termed methods.
-
The first example of a method we'll see is __init__.
-
__init__ allows us to add variables to a class as follows:
class Point:
""" Point class represents and manipulates x,y coords. """
def __init__(self, x=0, y=0):
""" Create a new point at the origin """
self.x = x # The self argument to method represents the
# object, and allows you to assign stuff to the object
self.y = y
p = Point(10, 12)
Object membership
class Point:
"""
Attributes:
x: float, the x-coordinate of a point
y: float, the y-coordinate of a point
"""
def __init__(self,x,y):
self.x = x
self.y = y
p = Point(10, 12)
print(p.x, p.y)
p.x = 5
p.y = 11
print(p.x, p.y)
Methods
0
Advanced issues found▲
- A way to add functionalities to an object
- The __init__() is an instance of a method, a function belonging to an object.
- We are free to add user defined methods:
class Point:
""" Create a new Point, at coordinates x, y """
def __init__(self, x=0, y=0):
""" Create a new point at x, y """
self.x = x
self.y = y
# We see the "self" argument again
def distance_from_origin(self):
""" Compute my distance from the origin """
# This is just Pythagorus's theorem
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
p = Point(3, 4)
print(p.distance_from_origin())
5 minutes break!
Objects vs. class variables
- The variables defined in the constructor are unique to an object:
p = Point(3, 4)
q = Point() # Make a second point
print(p.x, p.y, q.x, q.y)
# Each point object (p and q) has its own x and y
Objects vs. class variables
- If you want to create a variable shared by all objects you can use a class variable:
class Point:
# Class variables are defined outside of __init__ and are shared
# by all objects of the class
theta = 10
""" Create a new Point, at coordinates x, y """
def __init__(self, x=0, y=0):
""" Create a new point at x, y """
self.x = x
self.y = y
p = Point(3, 4)
q = Point(9, 10)
print("Before", p.theta, q.theta)
Point.theta = 20 # There is only one theta, so we just
# changed theta value for all Point objects -note the use of the class name
print("After", p.theta, q.theta, Point.theta)
Object mutability
0
Advanced issues found▲
- Python objects are mutable
p = Point(5, 10)
p.x += 5 # You can directly modify variables
# You can even add new variables to an object
p.new_variable = 1
print(p.new_variable)
# But note, this doesn't add a "new_variable" to other points
# you might have or create
q = Point(5, 10)
q.new_variable # This doesn't exist,
# because you only added "new_variable" to q
Modifier methods
class Point:
""" Create a new Point, at coordinates x, y """
def __init__(self, x=0, y=0):
""" Create a new point at x, y """
self.x = x
self.y = y
def move(self, deltaX, deltaY):
""" Moves coordinates of point
( this is a modifier method, which you call to
change x and y)
"""
self.x += deltaX
self.y += deltaY
p = Point()
p.move(5, 10)
print(p.x, p.y)
IS VS. ==
Example
Lecture 14 challenge
Questions?
CSE 20 - Lecture 14
By Narges Norouzi
CSE 20 - Lecture 14
- 1,152