loading...

Printing in D

jessekphillips profile image Jesse Phillips ・2 min read

Another excellent article on Python has me questioning why.

print("Hello Word”)

writeln("Hello Word");

This is where the similarities end. writeln is not a substitute for print as we see next.

print("my", "name", "is", "Austin")
# my name is Austin

writeln("my", "name", "is", "Jesse");
// mynameisJesse

While multiple arguments can be passed in, no default or explicit separator exists. Instead we can wrap our call in an array to get access to some array ops.

writeln(["my" , "name" , "is" , "Jesse"].join(" "));
// my name is Jesse

print(“my”, “name”, “is”, “Austin”, sep=“,”, end=“!”)
my,name,is,Austin!

write(["my" , "name" , "is" , "Jesse"].join(","), "!");
// my,name,is,Jesse!

In order to replicate the Python behavior, write was chosen because ln stands for line which is placed at the end. The exclamation mark is then included as the last argument.

with open(“text.txt”, “w”) as text:
print(“my”, “name”, “is”, “Austin”, sep=“,”, end=“!”, file=text)

with(File("text.txt", "w"))
    write(["my" , "name" , "is" , "Jesse"].join(","), "!");
writeln(readText("text.txt"));

Or

append("text.txt", ["my" , "name" , "is" , "Jesse"].join(",") ~ "!");

This uses a tilde (concatenation operator) because append does not take additional parameters like write.

https://run.dlang.io/

Conclusion

Python's choice to place a space between prints arguments likely lead to the choice of giving control of that separator. While the code to replicate Python's behavior is only minor, by making the default behavior meaningful to those wanting something readable printed to the screen Python required less familiarity of other language constructs.

If you instructed a new programmer "just wrap all your arguments in these brackets and call this join like so" as done in D, that new programmer would look at you and say "it's a computer why doesn't it do what I want instead of jumping through hoops.

Where I look at Python and see learning isolated tools. D's approach (also available in Python) is universally applicable. It can do a console and file, and other strings, networking, in some cases datatype.

Python tries to do the right thing but I think that opens the door for confusion because being explicit is not the norm.

Posted on by:

jessekphillips profile

Jesse Phillips

@jessekphillips

Senior Quality Assurance (SDET) ¶ Avid hobby D programmer ¶ Telling people what to do because I am right.

Discussion

pic
Editor guide
 

Your examples make D look like a pretty unreadable language.

with(File("text.txt", "w"))
    write(["my" , "name" , "is" , "Jesse"].join(","), "!");

There's no clear indication that you're working on the text.txt inside the with, and write just magically changes target.

 

Yeah, D programmers rearly utilize with even with limited scope it can hide the origin. The main thing here is to create a scope to automatically call close() like in Python.

{
auto f = File("text.txt", "w");
f.write(["my" , "name" , "is" , "Jesse"].join(","), "!");
} 

But D also provides an explicit cleanup since, not all cleanup is the same, and not everything is expected to clean up at scope exit

{
auto f = File("text.txt", "w");
scope(exit) f.close();
f.write(["my" , "name" , "is" , "Jesse"].join(","), "!");
} 
 

Nice post! I haven’t really seen much of D before this post, and I have to say it looks surprisingly readable!