[Tutor] constructors

Erik Price erikprice@mac.com
Thu, 11 Apr 2002 00:06:32 -0400


Okay, I had a whole bunch more questions about this constructors thread, 
so instead of flooding the list with a ton of individual emails, I have 
cut and paste everything into this one -- it responds to points made by 
Lloyd, Karthik, Alan, and Alexandre.

====

On Wednesday, April 10, 2002, at 10:23  AM, alan.gauld@bt.com wrote:

> That's the best reason. It ensures that the "pre conditions"
> of the objects methods are all met. Usually that translates
> to setting attributes but it could also be things like
> checking a hardware device was functioning correctly or
> even existed. This saves each and every function from doing the 
> precondition
> check itself. (Actually for hardware you
> probably should still check coz somebody might unplug it!)

So, if I had an class with a method that did a database access, then the 
constructor could do something like initialize the variables used in the 
class, and create a database connection.  If the database connection 
fails, then the constructor could return false, which would fail the 
creation of the object instance, so that we don't waste our time 
accessing the methods of the class since we already know we couldn't 
establish a database connection.

...does this sound right?

====

On Wednesday, April 10, 2002, at 08:20  AM, Alexandre Ratti wrote:

> I haven't used the new classes yet; I understand they allow you to 
> subclass build-in types such as lists or dictionaries. There are other 
> differences. See:
>
> http://www.amk.ca/python/2.2/index.html#SECTION000310000000000000000

This link, plus what dman explains, resembles what little I know of 
Java -- the new-style classes all descend from some other base class, 
which is "object" if there is no base class that is more suitable.  I 
haven't written extended classes yet, but I'll have to keep in mind that 
for the "new" style, I'll need to extend -something-, even if it has to 
be the generic "object".

====

On Wednesday, April 10, 2002, at 08:15  AM, Karthik Gurumurthy wrote:

> you would do cloning to achieve exactly what you wanted to..
> ie get a new instance (ie clone the instance) of a class from an 
> existing
> one with all it's attributes copied.
>
> Using the copy module happens to be a *standard* way of getting a copy 
> in
> python.
> So a client who uses your class will call copy.deepcopy() to achieve it
> since it is the norm.

I'm curious why you couldn't just assign a new reference to the object 
to get a copy.  OR.... would the new reference point to the SAME 
instance?  I just thought of this possibility now, as I wrote this.  Is 
that why you would want to copy an instance with this technique?

====

On Wednesday, April 10, 2002, at 08:22  AM, Lloyd Kvam wrote:

> Also note that Python supports an alternative way to have default 
> values.  You
> can define them at the class level.
>
> >>> class Person:
> ... 	name = "Not Specified"
> ... 	age = "Not Specified"
> ... 	
> >>> newguy = Person()
> >>> newguy.name
> 'Not Specified'
> >>> newguy.name = "Erik"
> >>> newguy.name
> 'Erik'
> >>> Person.name
> 'Not Specified'
>
> I have found class level defaults very useful for classes that represent
> database tables.  The class defaults can easily be built from 
> information
> provided by the database.

This technique of using class level defaults seems better than using 
default arguments to the constructor, since you can do something more 
complex to define the variables (like pull data from a database and use 
that as the defaults).  I haven't seen this before in Python -- in fact, 
I've only ever seen methods as part of a class, never standalone 
variables like this.

====

Thanks to all who have contributed to this thread, it's been very 
informative -- and, as with most of my questions, keeps bifurcating into 
two new questions for every answer I get!





Erik