Re: [Tutor] is it possible to call a setter property during classinstantiation?

2010-08-14 Thread ALAN GAULD


so we don't usually care too much about types. Are you sure you
>>really need all that type checking code? 

...I included a type check for a string to make sure that a future programmer 
(or myself) wouldn't try to pass in a list of lines after a text has been 
split. 

Other code internal to the Question class depends on the fact that the 
incoming data arrives in the form of a single string. 
Does it really? If so that suggfgests it will call a method that is unique to 
strings? 

If so an exception will be thrown that you can catch. but if your methods don't 
use 

methods that are unique to strings could you generalise them to be applicable 
to any sequence type - including a user defined class that looks like a string 
but is not derived from string?


If the wrong data type is fed, then I have a custom exception that gets raised 
>to specify the problem. I guess the program would just crash with a 
>TypeError if a list of lines was fed rather than a string, 
>Or you catch the error where the method really needs a string rather than 
>trying 
>
to predict the possibility in the init method. Of course this might mean many 
duplicated try/except clauses which might mess yopur code up just aws much. 
So its a trade off. LBYL v EAFP (try Google :-)


Is it better in such cases to just let the program die a natural death, 
>Or catch the error where it occurs and take remedial action. Would converting 
to a string work? (Usually not because almost anything in Python returns a 
value from str() even if only a description of type!)


Is there a better way to approach these issues?
>If you know which methods might be missing it can be more effective to 
check for their existence rather than checking for an explicit type. Thus 
code can check for a "file like object" by testing that the subset of file 
operations they need are present. This can often be combined with binding 
the methods to local variables for a slight performance improvement:

def f(aFile):
 try:   # we need to use read and close methods of the input object
_read = aFile.read
_close = aFile.close
 except AttributeError:
 raise TypeError
 data = _read()
 process(data)
 _close()

I'm not saying its never necessary to check types but its a lot less 
necessary than in static languages, especially if you try to write functions 
that can operate on the widest set of data types possible and guard 
against missing operations rather than "incorrect" types.

HTH,

Alan G.___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 78, Issue 68

2010-08-14 Thread Stephen Farthing
Guys,

Thanks for all the helpI am working my way through "Snake wrangling for
kids" as a start. I downloaded Netbeans and installed the plug in as Idle
will not load on my 64 bit Windows 7. And I have WXPython 2.8 plus the
examples to play with.

Life is good in rainy England,

Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 78, Issue 68

2010-08-14 Thread Bill Allen
On Sat, Aug 14, 2010 at 4:29 AM, Stephen Farthing wrote:

>
> Thanks for all the helpI am working my way through "Snake wrangling for
> kids" as a start. I downloaded Netbeans and installed the plug in as Idle
> will not load on my 64 bit Windows 7. And I have WXPython 2.8 plus the
> examples to play with.
>
> Life is good in rainy England,
>
> Steve
>
>
I also want to express my thanks for the help that I have received here on
Tutor.   My "practice program" is rapidly advancing now and I am having lots
of fun learning Python.   I have not enjoyed programming this much since I
had to figure out how to write my own graphics primitives in x86 Assembler
for an old IBM XT with a Hercules hires graphics card (very ancient home PC
tech) in order to write a program for generating Madelbrot fractals. (Yes
that was fun!) Thanks again!

Burning up in 100+ temps in Texas,
Bill
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] my Python learning exercise

2010-08-14 Thread Bill Allen
In the process of learning Python, I have given myself a programming
exercise to do.  This is a standard thing I do whenever learning a new
programming language and I have used this same exercise several times before
with other programming languages.  It is a simple text based game that
simply lets a user explore a maze of rooms.   If fully fleshed out, it would
be a Zork or D&D type game.  It demonstrates some basic programming tasks
such as reading data into an array from a file, getting and responding to
user input, looping, control structures, system calls and function
definitions.  I share it here as example code for others also learning
Python and I welcome comments.   The code is for Python 3.x, but the
comments explain how to easily convert it for use in Python 2.x.  It is far
from perfect and is for learning purposes only – something to tinker with.
It is based on concepts from Creating Adventure Games on Your Computer by
Tim Hartnell , which used the BASIC
programming language.   You are welcome to take this code and use and modify
for any purpose you like.  The current version of this code will always be
at this location:
mazegame-current.  You can
create an appropriate data file by making a text file with
contents similar to the following, the current code requires a fixed number
of columns (7 at the time of this writing and subject to change) and an
unlimited number of rows.
0 0 0 0 0 0 0
0 0 4 2 0 0 0
0 0 0 1 3 6 0
0 0 5 0 2 0 0
0 1 0 5 0 0 0
0 3 0 0 4 0 7
0 0 0 0 0 0 2
0 0 0 0 0 5 0
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor