Python Forum
function defs in decision code body
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function defs in decision code body
#1
i have been putting many function defs in decision bodies yo decide which version of a function to define. most cases involve testing the python version and defining function code specific for that version. the reason i do this is to reduce the version testing runtime cost, to do it when functions are being defined instead of each time the function is called. i consider it reasonable optimization (as opposed to some of the crazy hacks i've done in C and assembler to squeeze every cycle out of the CPU). i've also done things like decision code based on environment variables like my debug assistance functions that can be enabled (defined to do a specific help like finding and printing a variable) or disabled (defined to just return).

so i am wanting to get critical opinions about doing this kind of thing.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I try to avoid things like that, but sometimes it's necessary.

For python version testing, it never is.
If you need code that runs on both python 2 and 3, just use six.
Reply
#3
i have used six. it often makes my code more involved. even with six, coding it one way for python2 and another way for python3 comes up with more streamlined code. i'll be happy when everyone quits using python2. but many of these uses= cases cannot find a suitable OS system or distro with python3 that supports their needs. many of the changes in ubuntu, for example. make the newer versions unusable. i have encountered businesses that are unable to use any ubuntu since 8.04 due to some app they can't get fast enough in a VM.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020