[Tutor] Re: Logic error / poor understanding of classes?

Liam Clarke cyresse at gmail.com
Tue Nov 30 12:09:31 CET 2004


Erm... logic error. 

<embarassing logic error>

Please disregard this post, or use it to warn others of the danger of
coding while caffeine deprived.

when I loaded self.sortSetting, I forgot to add int() around it... *sigh*


On Tue, 30 Nov 2004 23:26:03 +1300, Liam Clarke <cyresse at gmail.com> wrote:
> Hi all,
> 
> Having an issue... creating a couple of methods in a class, and
> hitting a strange error.
> 
> def genWin(self):
>         self.Show(0)
> 
>         #Get dict vals of CM's and teams as (name, team) tuple
>         tuplesToGen=self.sortAndTuple()
>         print 'z point'
>         print tuplesToGen
> 
>     def sortAndTuple(self):
>         listOTuples=self.cmList.items()
>         print listOTuples
>         print 'List presort'
>         print self.sortSetting
> 
>         if self.sortSetting == 0:
>             print 'x point'
>             listOTuples.sort()
>             print 'y point'
>             print listOTuples
>             return listOTuples
> 
>         elif self.sortSetting == 1:
>             schwartzList = [ (item[1],item) for item in listOTuples ]
>             schwartzList.sort()
>             listOTuples  = [ item[1] for item in schwartzList ]
>             print'No, wrong'
>             return listOTuples
> 
> So this is within a class, and genWin calls sortAndTuple() to sort the
> list by alphabetical order of either value.
> 
> So, self.sortSetting is 0, so it should sort alphabetically... this is
> the console output I get -
> 
> [('Carla', 'ECM'), ('Dave', 'ABC') <etc. etc.> ]
> List presort
> 0
> z point
> None
> 
> So my expected list of tuples is fine, and self.sortSetting is 0, as
> it should be...
> 
> but, what happened to 'x' point and 'y' point? And also, why is my
> method returning None?
> self.sortSetting is not 0?
> 
> If I replace
> 
>  elif self.sortSetting == 1:
> 
> with else:
> 
> it runs that clause fine, so what am I doing wrong? Oh, and if I place a
> print self.sortSetting command in the else clause, it prints zero...
> 
> So, I know what the problem is, just not why it's happening.
> It is generating no error message for a missing var,  and I'm sure
> it's a quirk of classes or (more likely) my limited knowledge.
> 
> Can you not return values from one method to another in a class? Or,
> am I missing something like passing the calling function as a
> parameter i.e.
> 
> tuplesToGen=self.sortAndTuple(self)
> 
> or should I be passing self.sortSetting as an argument? I think I'm
> treating it as a global variable at the moment.
> 
> Regards,
> 
> (a Confused) Liam Clarke
> --
> 'There is only one basic human right, and that is to do as you damn well please.
> And with it comes the only basic human duty, to take the consequences.
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list