Python Stack Overflow
Python for beginners - Stack Overflow
How to design code in Python? - Stack Overflow
Am i still a python programmer if use stack overflow a lot and finish projects?
Hello! I am new to python. I created a script/bot, and its function is work until closed.
That bot has a main() function that one way or another calls itself after a 1 minute timer.
After some time i noticed that it crashed and closed Tested again but now in Visual Studio Code and after 18 hours it crashed again with fatal error stack overflow.
Went in and created a separate script with a main function calling itself and after 1035 loops it caused stack overflow. Now 1035*1minute is about 18 hours.
How can i create an infinite script without causing stack overflow?
Thanks!
This is a very healthy question.
Duck typing
The first thing to understand about python is the concept of duck typing:
If it walks like a duck, and quacks like a duck, then I call it a duck
Unlike Java, Python's types are never declared explicitly. There is no restriction, neither at compile time nor at runtime, in the type an object can assume.
What you do is simply treat objects as if they were of the perfect type for your needs. You don't ask or wonder about its type. If it implements the methods and attributes you want it to have, then that's that. It will do.
def foo(duck):
duck.walk()
duck.quack()
The only contract of this function is that duck exposes walk() and quack(). A more refined example:
def foo(sequence):
for item in sequence:
print item
What is sequence? A list? A numpy array? A dict? A generator? It doesn't matter. If it's iterable (that is, it can be used in a for ... in), it serves its purpose.
Type hinting
Of course, no one can live in constant fear of objects being of the wrong type. This is addressed with coding style, conventions and good documentation. For example:
- A variable named
countshould hold an integer - A variable
Foostarting with an upper-case letter should hold atype(class) - An argument
barwhose default value isFalse, should hold abooltoo when overridden
Note that the duck typing concept can be applied to to these 3 examples:
countcan be any object that implements+,-, and<Foocan be any callable that returns an object instancebarcan be any object that implements__nonzero__
In other words, the type is never defined explicitly, but always strongly hinted at. Or rather, the capabilities of the object are always hinted at, and its exact type is not relevant.
It's very common to use objects of unknown types. Most frameworks expose types that look like lists and dictionaries but aren't.
Finally, if you really need to know, there's the documentation. You'll find python documentation vastly superior to Java's. It's always worth the read.
I've reviewed a lot of Python code written by Java and .Net developers, and I've repeatedly seen a few issues I might warn/inform you about:
Python is not Java
Don't wrap everything in a class:
Seems like even the simplest function winds up being wrapped in a class when Java developers start writing Python. Python is not Java. Don't write getters and setters, that's what the property decorator is for.
I have two predicates before I consider writing classes:
- I am marrying state with functionality
- I expect to have multiple instances (otherwise a module level dict and functions is fine!)
Don't type-check everything
Python uses duck-typing. Refer to the data model. Its builtin type coercion is your friend.
Don't put everything in a try-except block
Only catch exceptions you know you'll get, using exceptions everywhere for control flow is computationally expensive and can hide bugs. Try to use the most specific exception you expect you might get. This leads to more robust code over the long run.
Learn the built-in types and methods, in particular:
From the data-model
str
join- just do
dir(str)and learn them all.
list
append(add an item on the end of the list)extend(extend the list by adding each item in an iterable)
dict
get(provide a default that prevents you from having to catch keyerrors!)setdefault(set from the default or the value already there!)fromkeys(build a dict with default values from an iterable of keys!)
set
Sets contain unique (no repitition) hashable objects (like strings and numbers). Thinking Venn diagrams? Want to know if a set of strings is in a set of other strings, or what the overlaps are (or aren't?)
unionintersectiondifferencesymmetric_differenceissubsetisdisjoint
And just do dir() on every type you come across to see the methods and attributes in its namespace, and then do help() on the attribute to see what it does!
Learn the built-in functions and standard library:
I've caught developers writing their own max functions and set objects. It's a little embarrassing. Don't let that happen to you!
Important modules to be aware of in the Standard Library are:
ossyscollectionsitertoolspprint(I use it all the time)loggingunittestre(regular expressions are incredibly efficient at parsing strings for a lot of use-cases)
And peruse the docs for a brief tour of the standard library, here's Part 1 and here's Part II. And in general, make skimming all of the docs an early goal.
Read the Style Guides:
You will learn a lot about best practices just by reading your style guides! I recommend:
- PEP 8 (anything included in the standard library is written to this standard)
- Google's Python Style Guide
- Your firm's, if you have one.
Additionally, you can learn great style by Googling for the issue you're looking into with the phrase "best practice" and then selecting the relevant Stackoverflow answers with the greatest number of upvotes!
I wish you luck on your journey to learning Python!
so i am sort of a python developer, i am an automation developer, my job is to automate tests, create framework etc.
but i find that i keep using stackoverflow a lot, question is am i still a programmer if i keep using stack overflow a lot?