[Tutor] scope, visibility?
Dave Angel
davea at ieee.org
Mon Nov 1 19:14:33 CET 2010
On 2:59 PM, Samuel de Champlain wrote:
> I am learning python. To practice, I am coding a hangman application in
> pyGTK.
> Here are my imports:
>
> import pygtk
> pygtk.require('2.0')
> import gtk
> import random
>
> Here is my main class:
>
> class PenduGTK:
>
> Inside the class is a method with a bit of code:
>
> def masque(chaine,liInd=0):
>
> i = 0
> lenght = len(chaine)
>
> The offending line is the one with len(chaine)
>
> Here are the error messages:
>
> penduGTK.py
> Traceback (most recent call last):
> File "/home/xxx/bin/penduGTK.py", line 23, in enter_callback
> self.lblMot.set_text(self.masque(self.motChoisi))
> File "/home/xxx/bin/penduGTK.py", line 44, in masque
> lenght = len(chaine)
> AttributeError: PenduGTK instance has no attribute '__len__'
>
> I would think it has to do with namespaces, scopes and visibility. But how
> do I refer to built-in functions from inside a class?
>
You're correctly referring to the built-in function len(). But that
function assumes that the object it gets as an argument has a __len__()
method. List, string, tuple all do. But perhaps PenduGTK does not.
You don't show us the whole class.
Your real problem is probably that you're missing self as the first
argument. So where you think chaine is a string, it's actually an instance.
DaveA
More information about the Tutor
mailing list