background preloader

Py: language

Facebook Twitter

LanguageComparisons. Comparing Python to various other languages: Full Treatments: External Articles: Multiple Languages: At Hyperpolyglot compare PHP, Perl, Python, Ruby and more side-by-side.

LanguageComparisons

Rosetta Code is a programming chrestomathy site. The idea is to present solutions to the same task in as many different languages as possible, to demonstrate how languages are similar and different, and to aid a person with a grounding in one approach to a problem in learning another. ... please add more! See also StrongVsWeakTyping. CategoryAdvocacy. Operators and String Formatting in Python. Although not actually modulus, the Python % operator works similarly in string formatting to interpolate variables into a formatting string.

Operators and String Formatting in Python

If you've programmed in C, you'll notice that % is much like C's printf(), sprintf(), and fprintf() functions. There are two forms of %, one of which works with strings and tuples, the other with dictionaries. StringOperand % TupleOperand StringOperand % DictionaryOperand Both return a new formatted string quickly and easily. % Tuple String Formatting In the StringOperand % TupleOperand form, StringOperand represents special directives within the string that help format the tuple.

>>> format = "%s is my friend and %s is %s years old" and creates two tuples, Ross_Info and Rachael_Info. >>> Ross_Info = ("Ross", "he", 28) >>> Rachael_Info = ("Rachael", "she", 28) The format string operator (%) can be used within a print statement, where you can see that every occurrence of %s is respectively replaced by the items in the tuple.

Format Directives The output is. Python: Lambda Functions. Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called "lambda".

Python: Lambda Functions

This is not exactly the same as lambda in functional programming languages, but it is a very powerful concept that's well integrated into Python and is often used in conjunction with typical functional concepts like filter(), map() and reduce(). This piece of code shows the difference between a normal function definition ("f") and a lambda function ("g"): As you can see, f() and g() do exactly the same and can be used in the same ways. Note that the lambda definition does not include a "return" statement -- it always contains an expression which is returned. Also note that you can put a lambda definition anywhere a function is expected, and you don't have to assign it to a variable at all. The following code fragments demonstrate the use of lambda functions. The following takes this a step further. Finally, reduce() is somewhat special.

Nic Young. Python provides a high level threading library that makes threading virtually painless.

Nic Young

Generally, you should only use threads if the following is true: Sharing memory between threads is not an issue.You are not looking for the best optimized performance since threads share memory within a process.You want to be able to share objects between threads.You take precautions that threads are not working on the same object at the same time. Anybody with any experience with threads know that you have to have a way to synchronize the tasks between the threads. Python once again makes this easy by providing the Queue data structure. The following will be an in depth look at one of the multi-threaded paradigms you can take advantage of using Python's built-in threading and Queue library. Producer and Consumer Paradigm A common design pattern when using threading in Python (or any other language) is to use the Producer and Consumer model of synchronizing threaded tasks.

The basic idea is this: Understanding Python's "with" statement. Fredrik Lundh | October 2006 | Originally posted to online.effbot.org Judging from comp.lang.python and other forums, Python 2.5’s new with statement (dead link) seems to be a bit confusing even for experienced Python programmers.

Understanding Python's "with" statement

As most other things in Python, the with statement is actually very simple, once you understand the problem it’s trying to solve. Consider this piece of code: set things up try: do something finally: tear things down Here, “set things up” could be opening a file, or acquiring some sort of external resource, and “tear things down” would then be closing the file, or releasing or removing the resource. If you do this a lot, it would be quite convenient if you could put the “set things up” and “tear things down” code in a library function, to make it easy to reuse.

Def controlled_execution(callback): set things up try: callback(thing) finally: tear things down def my_function(thing): do something controlled_execution(my_function) This wasn’t very difficult, was it?