[Tutor] constructors
Erik Price
erikprice@mac.com
Thu, 11 Apr 2002 23:02:34 -0400
On Thursday, April 11, 2002, at 03:14 PM, dman wrote:
> In either case, you can't
> return a value from __init__ -> the object already exists and is
> referenced by 'self', the __init__ just initializes the data in it.
> If the __init__ method completes, then the client gets the reference
> to the object. You can return 'false' or 'self' or 'fubar', but the
> client will never see it.
Ah. This is very important -- I was thinking that if the constructor
returned false, then the instance would not be created. Thank you.
> The do_something function doesn't need to do anything special (like
> check a return value and propagate it by returning it) to not ignore
> error conditions. The module-level code doesn't need to interrupt the
> flow of the code by checking a return value either, and can handle the
> same type of error from both functions identically (if it wants to).
This is nice. In PHP, I often do such code as:
if (!$connect = mysql_connect($connection_parameters)) {
die("Database connection failed");
}
if (!$result = mysql_query($sql, $connection_handle)) {
die("SQL query failed");
}
etc etc.
It gets very repetitive, and makes the code look ugly. But I hadn't
realized that this "interrupts the flow of code".
Exceptions are sweet -- I take it that they are another Python-centric
feature, that does not exist in C/C++/Java?
Erik