[Tutor] beginning to code
On Tue, 19 Sep 2017 09:53 pm, Rustom Mody wrote:
> How exceptional is python's choice to NOT raise exceptions can be seen by
You demonstrated that python raises exceptions for operations that aren't
defined or meaningful. I don't know what point you think that made, apart from
demonstrating that python is strongly typed.
You want a duck, but you have a dog, so when you ask it to quack, it can't and
raises an exception.
You want a duck, but all you have is a brick, so when you ask it to fly, it
can't, and raises an exception.
*Almost all* combinations of arbitrary objects with arbitrary operations are
doomed to fail. There's an infinite number of things you might want to do, and
most objects can't do more than a handful of them ("open a socket and send
these packets" -- "but I'm a float, I don't know how to open sockets").
I can only think of four operations which are plausibly universal:
Identity: compare two operands for identity. In this case, the type of the
object is irrelevant.
Kind: interrogate an object to find out what kind of thing it is (what class or
type it is). In Python we have type(obj) and isinstance(x, Type), plus a
slightly more specialised version issubclass.
Convert to a string or human-readable representation.
And test whether an object is truthy or falsey.
?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure
enough, things got worse.