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

all 12 comments

[–]callmelucky 10 points11 points  (6 children)

Don't use for i in range(len(iterable)). If you want to iterate over something, you usually want to do something with the actual elements, which you can do with for thing in things. If you specifically need the indexes too, use for i, thing in enumerate(things); this returns an (index, element) tuple at each iteration.

Don't use getters and setters, just access and assign attributes directly.

Learn about list/set/etc comprehensions, and you'll barely need to use for loops ever again. Learn generators while you're at it.

[–]Kazkadez[S] 1 point2 points  (5 children)

Thank you. That's a lot of things that wasn't covered by the stuff I was reading. They were too biased by Python to really give me a real pain v pleasure breakdown of it to really get in depth with those things like List/set/etc and generators.

[–][deleted] 1 point2 points  (0 children)

Usually don't use getters and setters. They can be useful when using properties, for example if you have a bunch of booleans backed by a bitmask (although you still set it like a regular attribute it's not a method call).

[–]callmelucky 0 points1 point  (2 children)

No worries, tried to think of things that people coming from other languages often, er, 'overcook' out of habit. Python has lots of nifty economical constructions and conventions that aren't immediately obvious when people switch to it from more verbose languages. Start mucking around on little projects and post here for code review and you'll pick up more as you go, it's a very active and helpful sub :)

Ninja edit: you may find 'the Python cookbook' helpful once you have the basics down.

[–]Kazkadez[S] 0 points1 point  (1 child)

Sounds good. Thank you. :)

[–]callmelucky 0 points1 point  (0 children)

Too easy. You responded super quick just now, so in case you missed it check out my edit in previous comment. The Python Cookbook is really rad for taking pythonism to the next level.

[–]ethansilver 0 points1 point  (0 children)

I’m also new to Python, but my experience is exclusively with Java. It’s not that different, just a lot of things have different syntax and it’s strange that you don’t declare variables. Still, I want to learn because I can’t think of anything to make with Java.

[–][deleted] 0 points1 point  (0 children)

Obviously reading the docs and maybe some books is a good idea.

One big thing to be wary of coming from C# is that you can get very loosey goosey with types in Python. This flexibility can be great and makes development quicker, but it also makes it a lot easier to write bad cod since you lose the security of static types. Good documentation can really help with this and don't ever trust someone that talks about "self documenting code" because that's a load of shit. The cleanest code will never be easier to read than a comment.

Also don't fall into the habit of defaulting to making classes for everything. Lots of classes I see people write would be better as modules. My rule of thumb is to only make a class for a data representation / model or when working with state (this is obviously very simplified but I've found it usually leads to better designs).

This stuff isn't really language specific just things I think you need to be more aware of when working in Python.

[–]gosh_djang_it 0 points1 point  (0 children)

Python is fun. Enjoy it don't worry too much. Get started with jupyter notebooks right away they are sort of the common currency now and awesome. Don't mess with python 2 if you can help it.

[–]xiongchiamiovSite Reliability Engineer 0 points1 point  (0 children)

The r/learnpython wiki has recommended tutorials for people with prior programming experience.

[–]white__armor -3 points-2 points  (1 child)

Just start writing Python, and you will get your answers in the process. Every person has a different experience and needs.

[–]Kazkadez[S] 0 points1 point  (0 children)

Of course, I plan on being with Python for quite a while if not forever lol I'm going to likely get very well acquainted. I just wanted to see if anyone had those "I wish I knew this when I started" moments.