When learning Python, is there a better way to write this?

I am learning Python (2.7) and checking what I have learned so far. I wrote a temperature converter that converts Celsius to Fahrenheit, and I wanted to know if my code could be written faster or something more than Pythonic. And can someone tell me if there is an actual name for if __name__ == '__main__': main() (out of curiosity)?

 from sys import argv, exit # import argv and exit functions def to_f(c): # Convert celsius to ferinheight temp = (c * 9/5) + 32 return temp def to_c(f): # Convert ferinheight to celsius temp = (f - 32) * 5/9 return temp def main(): args = argv[1:] # Creates an argument list omitting the omitting the [0] element if len(argv) < 2: exit(1) # If less than two arguments if args[0] == '-f': # If the first argument is -f print args[1], 'ferinheight is', str(to_c(int(args[1]))), 'celsius' elif args[0] == '-c': # If the first argument is -c print args[1], 'celsius is', str(to_f(int(args[1]))), 'ferinheight' else: exit(1) if __name__ == '__main__': main() 

http://pastebin.com/rjeNikDt

+4
source share
4 answers
 import sys def to_f(c): # Convert celsius to fahrenheit return (c * 9/5) + 32 def to_c(f): # Convert fahrenheit to celsius return (f - 32) * 5/9 def convert(args): if len(args) < 2: return 1 # If less than two arguments t = args[1] if args[0] == '-f': # If the first argument is -f print "%s Fahrenheit is %s Celsius" % (t, to_c(int(t))) return 0 elif args[0] == '-c': # If the first argument is -c print "%s Celsius is %s Fahrenheit" % (t, to_f(int(t))) return 0 else: return 1 if __name__ == '__main__': sys.exit(convert(sys.argv[1:])) 

What I've done:

  • Changed the name of main() to convert()
  • Pass arguments to convert() explicitly
  • Change the calls on exit() to return and call exit() in the main sentence.
  • You checked argv for a length of 2, when you had to check args .
  • The to_c and to_f do not need the temp variable, just return the expression.
  • While others are right, you can simply put the main () function at the top level, this is a good form of using the if __name__ style so that you can import this module and use the functions in other code.
  • Formatting strings is better than mixing strings and values ​​in a print statement. A.
  • args[1] appears enough that I assigned it t for brevity.
  • I prefer to import sys and use sys.argv , for example.
  • I always put dependent sentences on new lines, never if blah: doit()
  • Correct Spelling Fahrenheit
+11
source

Sample if __name__ == '__main__': designed for when you write a module intended for use by other code, but you want to get the testing code in the module.

If you run the module directly, it runs the material in this if block. If it is imported from another place, it is not.

So, I would recommend saving this block if __name__ == '__main__': because you can do something like:

 from temp_conv import c_from_f print c_from_f(73) 

later in another piece of code if you called this temp_conv.py.

+4
source

Several improvements in Ned's answer. In Python2.7 / , the result is truncated by default, so you need to import division from __future__ , otherwise (c * 9/5) + 32 always rounded, which leads to a decrease in accuracy.
for example, if 36C is 96.8F, it is better to return 97 than 96

You do not need the return statement in convert . By default, None returned. If there is a problem, you can throw an exception

Also using "".format() now preferable

A further improvement would be to use optparse or similarly handle command arguments, but may be redundant for such a simple program

 from __future__ import division import sys def to_f(c): # Convert celsius to fahrenheit return (c * 9/5) + 32 def to_c(f): # Convert fahrenheit to celsius return (f - 32) * 5/9 def convert(args): if len(args) != 2: raise RuntimeError("List of two elememts required") t = int(args[1]) if args[0] == '-f': # If the first argument is -f print "{0} Fahrenheit is {1} Celsius".format(t, round(to_c(t))) elif args[0] == '-c': # If the first argument is -c print "{0} Celsius is {1} Fahrenheit".format(t, round(to_f(t))) else: raise RuntimeError("First element should be -c or -f") if __name__ == '__main__': sys.exit(convert(sys.argv[1:])) 
+3
source
 import sys from getopt import getopt, GetoptError def to_f(c): return (c*9/5) + 32 def to_c(f): return (f-32) * 5/9 def usage(): print "usage:\n\tconvert [-f|-c] temp" def convert(args): opts = None try: opts, args = getopt(args, "f:c:") except GetoptError as e: print e if not opts or len(opts) != 1: usage() return 1 converters = { '-f': (to_c, '{0} Fahrenheit is {1} Celsius'), '-c': (to_f, '{0} Celsius is {1} Fahrenheit') } # opts will be [('-f', '123')] or [('-c', '123')] scale, temp = opts[0][0], int(opts[0][1]) converter = converters[scale][0] output = converters[scale][1] print output.format(temp, converter(temp)) return 0 if __name__ == '__main__': sys.exit(convert(sys.argv[1:])) 

I used getopt to clear your argument and error handling. I also combined the logic that acts on this parameter after this parameter is defined. Getopt is a very powerful parser of features, and I think it's worth exploring if you often write such programs.

+1
source

Source: https://habr.com/ru/post/1315343/


All Articles