[Tutor] Looking for some comments on my code segment...

Dave (NK7Z) dave at nk7z.net
Fri Feb 24 19:28:45 EST 2023


...and yet again, thank you for that input, and the examples...  All 
good advice...  Most of this is because I am new to Python...  I will 
implement.

I will make the changes you suggested, and hopefully those will trap 
errors better than my versions did...

THANK YOU again sir!!

73, and thanks,
Dave (NK7Z)
https://www.nk7z.net
ARRL Volunteer Examiner
ARRL Technical Specialist, RFI
ARRL Asst. Director, NW Division, Technical Resources

On 2/24/23 14:26, Cameron Simpson wrote:
> On 25Feb2023 09:18, Cameron Simpson <cs at cskk.id.au> wrote:
>> Two other things: by using global variables and calling exitscript() 
>> from your set up function you're embedding policy in the function i.e. 
>> that this function will only ever be called once and therefore globals 
>> can store the result, and that failure of this function should exit 
>> the entire programme.
> 
> I meant to add some commentry here:
> 
> I find it helpful to distinguish in my mind mechanism and policy. 
> Mechanism is how something is done eg: tn = telnetlib.Telnet(HOST, PORT, 
> TIMEOUT)
> 
> Policy is deciding how to handle the ways that might play out, 
> particularly failure.
> 
> By putting eg exitscript() in the function you're putting policy about 
> what happens on failure in the low level function opening the telnet 
> connection. This leaves little flexibility for the user of your function.
> 
> I know in this case it's a new script and you're the only person using 
> it, but later when you keep code around to solve problems it becomes 
> more and more important.
> 
> My general principle here is that mechanism lives in the low level 
> functions (well, it happens where it happens, or your programme doesn't 
> work), but policy should be in the outer levels i.e the caller of your 
> functions - as far out as possible.
> 
> One nice thing about exceptions is that they let you punt on policy as 
> much as you like. Don't know how some exception should be handled 
> (because _in_ the functin you don't know what the function's being used 
> for)? Ignore it and let the exception bubble out - some claller with 
> more context can decide what to do.
> 
> Cheers,
> Cameron Simpson <cs at cskk.id.au>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list