should error class be defined in the class it logically belongs?
Zhang Weiwu
tristan at realss.com
Sun Jan 5 03:35:01 EST 2014
Let's say we have in livestocks.py
# livestocks.py stores all livestock classes
class cat()
...
class Dog()
...
class CanineDistemper(Exception)
'''
<<< Buddy = Dog(name = "Buddy")
<<< raise CanineDistemper(Buddy) # horrible test code!
'''
def __init__(self, dog)
self.owner = dog # a specific dog
def __str__(self)
return "{} has had Canine Distemper".format(self.canine)
One would naturally wonder, since
1. by definition only a canine (a dog) can have CanineDistemper, and
2. in no case can CanineDistemper be raised without specifying which object
(canine) is having the problem,
Thus, CanineDistemper is an integral part of Canine class, and one is
naturally tempted to defined CanineDistemper inside Canine class.
Let some experienced OOP guy explain a bit if this line of thinking is
logical, or impratical. Thanks!
More information about the Python-list
mailing list