slides7-objects-classes
slides7-objects-classes
and Programming
Objects and Classes
Note that it might have been better to have each operation print
the accumulator value. What would you change?
General Form:
class ClassName:
initializer
methods
In file Circle.py:
import math
class Circle :
def __init__ ( self , rad = 1) :
""" Construct a Circle object with radius
rad ( defaults to 1) . """
self . radius = rad
...
>>> c1 = Circle ()
>>> c2 = Circle ( 5 )
class Circle :
def __init__ ( self , rad = 1) :
""" Construct a Circle object with radius
rad ( defaults to 1) . """
self . radius = rad
...
The other methods can refer to the class attributes using the dot
notation.
They have self as a parameter at definition. When they are called
on a class instance (object), self is an implicit parameter referring
to the object itself.
>>> c1 . getRadius () # self references c1
5
>>> c1 . getPerimeter ()
31.41592653589793
Even with setters and getters, there’s nothing to prevent code from
accessing attributes directly, unless you make the attribute private.
An attribute beginning with two underscores is private to the class.
import math
class Circle :
# Construct a circle object , with radius
# a private attribute .
def __init__ ( self , rad = 1) :
self . __radius = rad
The only access to __radius outside the class is via the getter
and setter methods.
CS303E Slideset 7: 22 Objects and Classes
Code Defensively: Reconsider Circle Setter
Data:
Rank: ”Ace”, ”2”, ”3”, ”4”, ”5”, ”6”, ”7”, ”8”, ”9”, ”10”,
”Jack”, ”Queen”, ”King”
Suit: ’Spades’, ’Diamonds’, ’Hearts’, ’Clubs’
Methods:
Tell me your rank.
Tell me your suit.
How would you like to be printed?
def isRank ( r ) :
# Recognizer for a legal rank :
return r == ’ Ace ’ or r == ’2 ’ or r == ’3 ’ or r == ’4 ’ \
or r == ’5 ’ or r == ’6 ’ or r == ’7 ’ or r == ’8 ’ \
or r == ’9 ’ or r == ’ 10 ’ or r == ’ Jack ’ \
or r == ’ Queen ’ or r == ’ King ’
def isSuit ( s ) :
# Recognizer for a legal suit
return s == ’ Spades ’ or s == ’ Diamonds ’ \
or s == ’ Hearts ’ or s == ’ Clubs ’
def c ar dI nd e xT oR an k ( i ) :
if i == 0: return ’ Ace ’
elif i == 1: return ’2 ’
...
elif i == 12: return ’ King ’
else :
print ( " Not legal index for rank : " , i )
In file/module Card.py
class Card :
""" A card object with a suit and rank . """
def __init__ ( self , rank , suit ) :
""" Create a Card object with the given rank
and suit . """
if ( not isRank ( rank ) or not isSuit ( suit ) ) :
print ( " Not a legal card specification . " )
return
self . __rank = rank
self . __suit = suit
This tells print what string to display if you ask to print a Card
object.
Notice that we’re comparing cards only according to rank, and Ace
is less than 2. Think how you’d define a more robust test.
You can use all of the standard relational operators assuming you
have defined __lt__ and __le__ so Python can figure out what
you mean. You can always do equality comparison X == Y , which
will be the same as “is” (same object in memory) unless you define
__eq__.
You can also define __gt__ and __ge__ but be careful that your
definitions form a consistent collection.
>>> x = [1, 2, 3]
>>> y = x
>>> z = [1, 2, 3]
>>> x == y
True
>>> x is y
True
>>> x == z
True
>>> x is z
False