I’m reading this book called Master Object Oriented programming Python by Steven Lott.
I came across this code:
class Card:
def __init__( self, rank, suit ):
self.suit= suit
self.rank= rank
self.hard, self.soft = self._points()
class NumberCard( Card ):
def _points( self ):
return int(self.rank), int(self.rank)
class AceCard( Card ):
def _points( self ):
return 1, 11
class FaceCard( Card ):
def _points( self ):
return 10, 10
I’m not able to understand how can self._points()
in Card be legal expression.
I run the code in compiler too it stats the following error Instance of Card has No points member
Full Code I have kept it as gist here
Advertisement
Answer
From your error: it is just a pylint issue, if you do not do something like c = Card(1, 2)
, it will be ok.
The code in fact is correct.
It intends you will call it like n = NumberCard(1, 2)
, then __init__
will be used by its subclass, and subclass has the function _points
.
In fact, for other language, usually we need to define a _points
function in parent class. Then, subclass could override the function in parent class. But python
is not so strict, so you could do without define _points
function in parent class, but then you will just able to new a object for its subclass not parent class.
To sum all, what you need to google is function override
of OOP.