Why Python language does not have a writeln() method?

前端 未结 5 463
情话喂你
情话喂你 2020-12-30 23:47

If we need to write a new line to a file we have to code:

file_output.write(\'Fooo line \\n\')

Are there any reasons why Python does not ha

5条回答
  •  囚心锁ツ
    2020-12-31 00:40

    print() is the function you are looking for. Like much of the core polymorphism in python, printing is provided by a built-in function instead of using methods (just like len(), next(), repr() etc!).

    The print() function being the universal interface also makes it more versatile, without the file objects themselves having to implement it. In this case, it defaults to terminating by newline, but it can be chosen in the function call, for example:

    print("text", file=sys.stderr, end="\n")
    

    In your suggested use case, all file objects would have to implement not only a .write() method (now used by print()), but also .writeln(), and maybe even more! This function-based polymorphism makes python very rich, without burdening the interfaces (remember how duck typing works).

    Note: This model has always been at the center of Python. It is only more pure in my examples, that pertain to Python 3

提交回复
热议问题