newb comment request

Peter Otten __peter__ at web.de
Fri Nov 28 05:14:40 EST 2003


Alexandre wrote:

[Peter]
>> Object oriented programming is about programming against interfaces, so
>> exessive type checking is a strong hint to design errors.

[Alexandre] 
> I'm not sure i understand... well, let's put it that way : i'm sure i
> don't understand :) I guess i'll have to read about OO design... this is
> my first program, although i think i understand what OO means, i'm not
> abble to write OO yet :/

Type checking is generally best avoided, e. g. if you test for

isinstance(mystream, file)

this may fail on mystream objects that have all the methods needed to
replace a file in subsequent code and you thus unnecessarily limit its
usage.
As your script is explicitly in the "type checking business", my remark was
a bit off, though.

>> Where is valuesD introduced?
> Not sure i understand the question... i used this variable name "ValuesD"
> (meaning Values from Data) not to conflict with the other variable named
> "values" in the same function.

I spotted an error where there was none - nothing to understand here :-(

>> Module level code is habitually wrapped like so:
>>
>> if __name__ == "__main__":
>>     infile = open('cached-objects-Python-pickled-sample', 'rb')
>>     _data = pickle.load(infile)
>>     infile.close()
>>     # process _data...
>>
>> That way, you can import the module as well as use it as a standalone
>> script.
> 
> Oh ? but, i can use it as standalone script ?!
> If i type myScriptName.py in my dos prompt the script is working !

Yes, but you can *only* use it as a standalone script. If you import it into
another module, it will try to read the pickled data from the hard-coded
file before you can do anything else.


Peter






More information about the Python-list mailing list