Skip to content

Basics notes #5

@parthsarin

Description

@parthsarin

1. Style guide violations

Explanations in Code

The style guide specifies that we should not have explanations in code. You do this once, in the file reading section:

file_text = f.read(size) # Reads size bytes from the file; if size is omitted, the entire file is read.
first_line = f.readline() # Reads the file one line at a time.
all_lines = f.readlines() # Returns a list with all lines of the file.

Printed Output

The style guide specifies

When an expression will result in printed terminal output, this should be written after the expression and a blank line. Each line of output should be commented out with the hash sign at the beginning of the line, a space, and then the terminal output. The implication sign should not be included.

But many of your examples use the implication sign to offset printed output, like

print("I'm dancing.")              # => I'm dancing.
print('"This is fun!" he cried.')  # => "This is fun!"

Maybe remove the print statements or convert the implication comments to print-style comments?

Italics

You frequently use italics to denote terms when they're introduced.

Nitpicky Spacing

You're using two spaces instead of one in many of the return value implicatures

math.gcd(45, 105)  # => 15
math.cos(3.14)     # => -0.9999987317275395
math.cos(math.pi)  # => -1.0

I know this is very nitpicky but... let's be consistent?

2. File I/O with open/close

You wrote

It's almost always a better idea to use the paradigm of the context manager when performing File I/O in Python. Cases where it is superior to use the open/close paradiem are significantly fewer - only use it if you have a clear, good reason to do so.

I'm curious: can you think of any time where it's superior to use the open/close paradigm?

Reading Bytes

f.read(size) reads size many bytes only when the file is open in bytes mode and size many characters otherwise.

3. C-Style Formatting

Why are we showing them C-style formatting at all if we immediately move on to say "never do this"?

Also, this code is incorrect:

print("{0} then {1} then {0} then {1} then {0}." % ("Parth", "Michael"))  # => Parth then Michael then Parth then Michael then Parth

4. Add Filenames

When a file is introduced, I think we should add filenames at the top to make it clear that this is happening inside a file.

"""
File: hello.py
--------------
"""
...

5. Revisit the Signature

Let's have a consistent signature.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions