Key features pythonic?

I just fit into Python coding and I wonder what is considered more pythonic? Example A: The obvious basic method.

#!/usr/bin/env python -tt import random def dice_roll(num=1): for _ in range(num): print("Rolled a", random.randrange(1,7,1)) def main() random.seed() try: num = int(input("How many dice? ")) dice_roll(num) except ValueError: print("Non-numeric Input") if __name__ == '__main__': main() 

or Example B: There is no basic method.

 #!/usr/bin/env python -tt import random def dice_roll(num=1): for _ in range(num): print("Rolled a", random.randrange(1,7,1)) if __name__ == '__main__': random.seed() try: num = int(input("How many dice? ")) dice_roll(num) except ValueError: print("Non-numeric Input") 

Any tips / pointers would be appreciated?

+4
source share
3 answers

Better because it allows you to import your module and execute stuff in main without having to randomly redo things. In fact, this may be a good reason to call it something better than main if it really is the actual function of your module.

+8
source

Well, when it comes to Pythonic, I would say that they are both the same Pythonic, because this refers to specific programming conventions and does not (and should not) regulate the way the coding is done.

However, usually people use the first form, since it is easier to import and call a script from another.

+3
source

I would say Example A is more Pythonic, but both are acceptable. Mostly because you can import the main one, but usually you do not need to do this.

+1
source

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


All Articles