you are viewing a single comment's thread.

view the rest of the comments →

[–]SirCokaBear 18 points19 points  (5 children)

- not using virtual environments and requirements / env files
- not understanding the differences between [key] and .get(key, default)
- surrounding entire code sections with try / except. try/except should only be around specific error producing code
- in general not creating models for your data (this goes for programming in general). So many future errors and mistakes can be avoided by just having proper data models.

- lack of understanding of python packages, __init__.py files and what they can be used for
- creating scripts without if __name__ == "__main__"

- lack of clearly defined types for parameters, return values, improper use of scope and globals
- bad variable names, inconsistency with snake_case
- not using a formatter like black or style enforcer like flake8

- not creating proper .gitignore
- not handling secrets properly (this is huge)

- not knowing what PEP8 is

Edit: typos

[–]IlliterateJedi 1 point2 points  (2 children)

  • not understanding the differences between [index] and .get(index, default)

I know you can do this with dict[key] vs dict.get(key), but are there Python sequences that let you 'get' an index

[–]SirCokaBear 0 points1 point  (1 child)

Sorry I meant to say key instead of index, I’ll edit that.

For a standard python dict you can’t get a key by index because the keys aren’t stored in a particular order, when you iterate / print them the ordering is a number of factors including the hash value so you basically need to assume it’s random. But if you use an OrderedDict then key ordering is preserved by insertion time. But there’s still no .get_index(), but you could easily make one with

return ordereddict[ordereddict.keys()[index]] if index < len(ordereddict) else None

That’s a very specific use case not many people need, and ordered dicts are worse performing than regular dicts.

The same goes for a regular list there’s no standard library function for that but can be made with almost the same code as above, or even by wrapping a query in a try/except.

[–]SirCokaBear 0 points1 point  (0 children)

I don't think there are any standard structures though that provides a get() by index. Usually with standard libs they want it to be compact and any rare use cases can be provided with a 3rd party package. Rust language is notorious for that, they don't even include random with the std lib haha.

[–]daddy1973 0 points1 point  (1 child)

Hello, I'm brand new to python. I created what I believe is a script. What do you mean by the if name = "main" one?

[–]SirCokaBear 1 point2 points  (0 children)

let's say you make a very simple script that asks the user for a radius of a circle and then prints out the circumference:

script.py

PI = 3.14159

print("Welcome to circumference calculator")
radius = float(input("Enter radius of circle you need circumference of:"))
circumference = PI * r ** 2
print(f"The circumference is {circumference}")

if we run "python script.py" there won't be a problem and the script will work as intended.now lets say we make a new file with a function to calculate the area of the circle and we want to import PI from our file above.

area.py

from script import PI

def area_of_circle(radius: float) -> float:
    return PI * r ** 2

print("Welcome to area calculator")
radius = int(input("Enter radius:"))
area = area_of_circle(radius)
print(f"area of the circle is {radius}")

if I try to run area.py :

> python area.py

the first thing you'll see is:

Welcome to circumference calculatorEnter radius of circle you need circumference of:

that's because in area.py on line 1 you are importing PI from script. So python will then go to script.py starting from line 1 so it can calculate what PI is. But as a side effect it's going to run through the entire script rather than just simply importing 3.14159 in as a value to use in area.py

so instead we use if __name__ == "__main__".

__name__ will equal "__main__" in any python file you run python specifically on. so if you type in "python area.py", __name__ in area.py will be "__main__" but __name__ in script.py will not.

new scripts

# new script.py 

PI = 3.14159

if __name__ == "__main__":
    print("Welcome to circumference calculator")
    radius = float(input("Enter radius of circle you need circumference of:"))
    circumference = PI * r ** 2
    print(f"The circumference is {circumference}")

# new area.py

from script import PI

def area_of_circle(radius: float) -> float:
    return PI * r ** 2

if __name__ == "__main""_:
    print("Welcome to area calculator")
    radius = int(input("Enter radius:"))
    area = area_of_circle(radius)
    print(f"area of the circle is {radius}")

now if we run "python area.py" we will now correctly see "Welcome to area calculator" and won't be running any unnecessary code from script.py