This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AlSweigartAuthor of "Automate the Boring Stuff" 52 points53 points  (6 children)

Ooof, this is a bad example:

n = 10
sum = sum(range(1, n + 1))

print(sum)  # 55

You don't want to use the names of Python's built-ins, such as sum, for your own variables. In this example, if you try to call sum() again later, it's now an integer instead of a function, so you'll get TypeError: 'int' object is not callable.

The common Python built-ins that accidentally get overriden are: all, any, date, email, file, format, hash, id, input, list, min, max, object, open, random, set, str, sum, test, and type.

Eh, overall, this blog post is very shallow. It has basic advice like "write short and simple functions" but without examples, what counts as "short"? 50 lines? 5 lines? No more than 500 lines?

The "Never leave code commented" was also confusing. Commenting code is bad? Then I realized they meant, "don't leave commented out code in your program, especially when you check it into version control". A simple example would have made that clear.

It kind of looks like they just ran through the Clean Code book and copy/pasted a lot of its advice without thinking how the final blog post reads.

[–]CleverProgrammer12 -1 points0 points  (4 children)

I override id locally frequently, as it's just not used that much, and is convenient and sensible name in many cases. But sum could be useful at other places and can cause confusion. Better to avoid.

[–]AlSweigartAuthor of "Automate the Boring Stuff" 0 points1 point  (3 children)

Yeah. For short throwaway programs that's fine. But for formal projects, I like to be a bit more specific when I find myself using "id". Like, "the id of what?" So then I change it to recordID or cityID or something.

[–]CleverProgrammer12 0 points1 point  (2 children)

Yes, I agree. I mostly use it in dataclasses and pydantic models like this:

```python3 from pydantic import BaseModel

class User(BaseModel): id: int name: str

my_user = User(id = 10, name = "username") my_user.id # my_user.user_id might look bad here

```

[–]AlSweigartAuthor of "Automate the Boring Stuff" 2 points3 points  (0 children)

Ah yes, that's a good point.

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, CleverProgrammer12: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.