[Tutor] Tutor Digest, Vol 81, Issue 3

TGW galaxywatcher at gmail.com
Tue Nov 2 01:37:13 CET 2010


rgr.
On Nov 1, 2010, at 5:21 PM, tutor-request at python.org wrote:

> Send Tutor mailing list submissions to
> 	tutor at python.org
> 
> To subscribe or unsubscribe via the World Wide Web, visit
> 	http://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
> 	tutor-request at python.org
> 
> You can reach the person managing the list at
> 	tutor-owner at python.org
> 
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
> 
> 
> Today's Topics:
> 
>   1. Re: scope, visibility? (Dave Angel)
>   2. Re: scope, visibility? (Evert Rol)
>   3. Re: Problems with partial string matching (Josep M. Fontana)
>   4. Complete Shutdown (Chris King)
>   5. rights (Chris King)
>   6. Re: rights (Vince Spicer)
>   7. Re: Complete Shutdown (Alan Gauld)
> 
> 
> ----------------------------------------------------------------------
> 
> Message: 1
> Date: Mon, 01 Nov 2010 14:14:33 -0400
> From: Dave Angel <davea at ieee.org>
> To: Samuel de Champlain <samueldechamplain at gmail.com>
> Cc: tutor at python.org
> Subject: Re: [Tutor] scope, visibility?
> Message-ID: <4CCF0389.10702 at ieee.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 
> 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
> 
> 
> 
> ------------------------------
> 
> Message: 2
> Date: Mon, 1 Nov 2010 19:15:49 +0100
> From: Evert Rol <evert.rol at gmail.com>
> To: Samuel de Champlain <samueldechamplain at gmail.com>
> Cc: tutor at python.org
> Subject: Re: [Tutor] scope, visibility?
> Message-ID: <054046EC-CC0E-48B4-A85E-083FCE7EE5AB at gmail.com>
> Content-Type: text/plain; charset=us-ascii
> 
>> 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__'
> 
> A method takes as its first argument a reference to the class. That is, in 'def some_method(blah1, blah2, blah3)', blah1 would be a reference to the class in which some_method is defined.
> If you look at the error message, you see Python complains that the PenduGTK instance has no __len__ attribute, meaning 'chaine' is a PenduGTK instance: it's the first argument in the method, taking a reference to the class.
> See eg http://diveintopython.org/object_oriented_framework/defining_classes.html
> (also, carefully read the error and think what it could mean: it has a lot of hints to solve your problem).
> 
> Thus, define a method with an extra (first) argument. This is usually called self:
> 
>    def masque(self, chaine, liInd=0):
> 
> 
> Last note on a totally different thing, because this confused me a bit: preferably avoid avoid the lowercase L, lowercase i and uppercase I next to each other. It's very hard to read. See eg http://www.python.org/dev/peps/pep-0008/ (section 'Names to Avoid'). To me, it initially read something like iiind=0. Which is ok, as long as I don't have to work with the code. But it may also bite you some day.
> 
> 
>> 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?
> 
> Just as you did above: len(chaine). 
> But since that wasn't your problem, I guess this answer is rather meaningless.
> 
> Cheers,
> 
>  Evert
> 
> 
> 
> ------------------------------
> 
> Message: 3
> Date: Mon, 1 Nov 2010 20:38:06 +0100
> From: "Josep M. Fontana" <josep.m.fontana at gmail.com>
> To: Dave Angel <davea at ieee.org>
> Cc: tutor at python.org
> Subject: Re: [Tutor] Problems with partial string matching
> Message-ID:
> 	<AANLkTi=rqJguU1NMH6+dt0gTKuHr6drSTo2NR42CxR81 at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
> 
>> The only time year is bound is in the previous loop, as I said. ?It's the
>> line that goes:
>> ? ? name, year = line.strip.....
>> 
>> So year is whatever it was the last time through that loop.
> 
> OK, this makes sense. Indeed that is the value in the last entry for
> the dictionary. Thanks a lot again.
> 
> Josep M.
> 
> 
> ------------------------------
> 
> Message: 4
> Date: Mon, 01 Nov 2010 16:01:39 -0400
> From: Chris King <g.nius.ck at gmail.com>
> To: python mail list <tutor at python.org>
> Subject: [Tutor] Complete Shutdown
> Message-ID: <4CCF1CA3.3050305 at gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 
>  Dear Tutors,
>     How do I completely shutdown a computer without administrative 
> rights using a simple python script.
> Sincerely,
>     Me, Myself, and I
> 
> 
> ------------------------------
> 
> Message: 5
> Date: Mon, 01 Nov 2010 16:05:57 -0400
> From: Chris King <g.nius.ck at gmail.com>
> To: python mail list <tutor at python.org>
> Subject: [Tutor] rights
> Message-ID: <4CCF1DA5.2020904 at gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 
>  Dear Tutors,
>     How do you give a script right to read a folder?
> 
> 
> ------------------------------
> 
> Message: 6
> Date: Mon, 1 Nov 2010 14:10:43 -0600
> From: Vince Spicer <vince at vinces.ca>
> To: Chris King <g.nius.ck at gmail.com>
> Cc: python mail list <tutor at python.org>
> Subject: Re: [Tutor] rights
> Message-ID:
> 	<AANLkTimQ_A5ontWy7yKf_nuwy0_iunkvH9Qro-mShbUD at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> On Mon, Nov 1, 2010 at 2:05 PM, Chris King <g.nius.ck at gmail.com> wrote:
> 
>> Dear Tutors,
>>   How do you give a script right to read a folder?
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>> 
> 
> 
> Which Operation System?
> 
> In linux the user that is running the script must be have read access
> 
> chmod +r folder
> 
> 
> -- 
> Vince Spicer
> 
> -- 
> Sent from Ubuntu
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://mail.python.org/pipermail/tutor/attachments/20101101/61ad9dd4/attachment-0001.html>
> 
> ------------------------------
> 
> Message: 7
> Date: Mon, 1 Nov 2010 21:20:42 -0000
> From: "Alan Gauld" <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Complete Shutdown
> Message-ID: <ianavd$osg$1 at dough.gmane.org>
> Content-Type: text/plain; format=flowed; charset="iso-8859-1";
> 	reply-type=response
> 
> 
> "Chris King" <g.nius.ck at gmail.com> wrote
> 
>>    How do I completely shutdown a computer without administrative 
>> rights using a simple python script.
> 
> If you have such a computer get rid of it, it fails the most basic 
> test
> of a secure operating system. No program that runs upon it could
> ever be relied upon!
> 
> The whole concept is evil.
> 
> Administrator rights are required for good reason and are a protection
> against bad things happening to your data and programs. Work with
> it not against it and be grateful it's there.
> 
> 
> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> 
> 
> 
> 
> ------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> End of Tutor Digest, Vol 81, Issue 3
> ************************************



More information about the Tutor mailing list