background preloader

Blogs-python

Facebook Twitter

Common Gotchas. For the most part, Python aims to be a clean and consistent language that avoids surprises. However, there are a few cases that can be confusing to newcomers. Some of these cases are intentional but can be potentially surprising. Some could arguably be considered language warts. In general though, what follows is a collection of potentially tricky behavior that might seem strange at first glance, but is generally sensible once you’re aware of the underlying cause for the surprise.

Mutable Default Arguments Seemingly the most common surprise new Python programmers encounter is Python’s treatment of mutable default arguments in function definitions. What You Wrote def append_to(element, to=[]): to.append(element) return to What You Might Have Expected to Happen my_list = append_to(12)print my_list my_other_list = append_to(42)print my_other_list A new list is created each time the function is called if a second argument isn’t provided, so that the output is: What Does Happen Late Binding Closures.

Redirecting output of Python programs to log file in Supervisor. The Top Mistakes Developers Make When Using Python for Big Data Analytics. The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) By Joel Spolsky Wednesday, October 08, 2003 Ever wonder about that mysterious Content-Type tag? You know, the one you're supposed to put in HTML and you never quite know what it should be? Did you ever get an email from your friends in Bulgaria with the subject line "???? I've been dismayed to discover just how many software developers aren't really completely up to speed on the mysterious world of character sets, encodings, Unicode, all that stuff.

But it won't. So I have an announcement to make: if you are a programmer working in 2003 and you don't know the basics of characters, character sets, encodings, and Unicode, and I catch you, I'm going to punish you by making you peel onions for 6 months in a submarine. And one more thing: In this article I'll fill you in on exactly what every working programmer should know. A Historical Perspective The easiest way to understand this stuff is to go chronologically. And all was good, assuming you were an English speaker. Unicode Hello Encodings Right?

Operator Overloading in Python. Python is an interesting language. It’s meant to be very explicit and easy to work with. But what happens when how you want or need to work with Python isn’t just what the native types expose? Well, you can build classes and give those classes attributes or methods that let you use them, but then you end up having to call methods instead of being able to just add items together where it makes sense. But what if you could just add them together?

What if your Area class instances could just be added together to make a larger area? Well, thankfully, you can. Remember back in Object-Oriented Python when we overloaded __init__() to change how our classes initialized themselves? Book Club Many of us here at Treehouse like to read and I think it would be neat to have a way to measure the books we’ve read. Class Book: title = '' pages = 0 def __init__(self, title='', pages=0): self.title = title self.pages = pages def __str__(self): return self.title Reverse Adding OK, let’s try it.

Excellent! Improve Your Python: Decorators Explained. I've previously written about "yield" and generators. In that article, I mention it's a topic that novices find confusing. The purpose and creation of decorators is another such topic (using them, however, is rather easy). In this post, you'll learn what decorators are, how they're created, and why they're so useful. A Brief Aside... Passing Functions Before we get started, recall that everything in Python is an object that can be treated like a value (e.g. functions, classes, modules). Def is_even(value): """Return True if *value* is even. """ return (value % 2) == 0 def count_occurrences(target_list, predicate): """Return the number of times applying the callable *predicate* to a list element returns True. """ return sum([1 for e in target_list if predicate(e)]) my_predicate = is_evenmy_list = [2, 4, 6, 7, 9, 11]result = count_occurrences(my_list, my_predicate)print(result) The magic is in the lines my_predicate = is_even.

Hopefully, this is all old hat to you. Returning Functions Raw Power. How can I make a chain of function decorators in Python? Python Re Match, Search Examples. Debugging Python Like a Boss. Life is short - you need Python!