Confusion regarding constructor as default value

aine_canby at yahoo.com aine_canby at yahoo.com
Thu Sep 27 09:42:03 EDT 2007


On 27 Sep, 15:03, aine_ca... at yahoo.com wrote:
> Why are the following different?
>
> def AddRow(self, rowName, tableRow=TableRow(ReleaseDate(""),
> ExpiryDate(""))):
>                 # check to see if the row already exists, if not add it to the
> container
>
>                 if not self.dict.has_key(rowName):
>                         self.dict[rowName] = tableRow
>
> def AddRow(self, rowName, tableRow):
>                 # check to see if the row already exists, if not add it to the
> container
>
>                 if not self.dict.has_key(rowName):
>                         self.dict[rowName] = TableRow(ReleaseDate(""), ExpiryDate(""))
>
> It seems that when I use the first function that I'm getting duplicate
> objects in self.dict
>
> Thanks for your help,
>
> Aine.

I've just tried the following:

Function A:

def AddRow(self, rowName, tableRow=TableRow(ReleaseDate(""),
ExpiryDate(""))):

    print tableRow
    if not self.dict.has_key(rowName):
        self.dict[rowName] = tableRow

Function B:

def AddRow(self, rowName, tableRow=None):

    if tableRow==None:
    	tableRow = TableRow(ReleaseDate(""), ExpiryDate(""))
    # check to see if the row already exists, if not add it to the
container
    print tableRow
    if not self.dict.has_key(rowName):
        self.dict[rowName] = tableRow

Function A is giving:

<DatabaseExamination.TableRow instance at 0x011D4468>
<DatabaseExamination.TableRow instance at 0x011D42B0> same!
<DatabaseExamination.TableRow instance at 0x011D42B0> same!
<DatabaseExamination.TableRow instance at 0x011D42B0> same!
<DatabaseExamination.TableRow instance at 0x011D42B0> same!

Function B is giving:

<DatabaseExamination.TableRow instance at 0x011D0670>
<DatabaseExamination.TableRow instance at 0x011D0760>
<DatabaseExamination.TableRow instance at 0x011D07D8>
<DatabaseExamination.TableRow instance at 0x011D0850>


So at least I know know what is causing the problem. But I'm still not
understanding why I can't use TableRow() as the default value in order
to geterate new instances.

How would you implement this?

Thanks,

Aine.




More information about the Python-list mailing list