ˇ@

Home 

python Programming Glossary: so

What is a metaclass in Python?

http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python

just OO concepts but this one is tricky. I know it has something to do with introspection but it's still unclear to me... that in Python. Classes are objects too. Yes objects. As soon as you use the keyword class Python executes it and creates.. this class __main__.Foo object at 0x89c6d4c But it's not so dynamic since you still have to write the whole class yourself...

Fastest way to list all primes below N in python

http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python

from the set. Nevertheless it works at least for me for some input numbers sum get_primes 2000000 142913828922L #That's.. 1000 False 529 in get_primes 530 True EDIT The rank so far pure python no external sources all primes below 1 million.. 530 True EDIT The rank so far pure python no external sources all primes below 1 million Sundaram's Sieve implementation..

The Python yield keyword explained

http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained

for metric spaces. This is the link to the complete source http well adjusted.de ~jrschulz mspace . python iterator.. When you use a list comprehension you create a list and so an iterable mylist x x for x in range 3 for i in mylist ..... Add the children of the candidate in the candidates list # so the loop will keep running until it will have looked # at all..

Flattening a shallow list in Python

http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python

this question. Here is a summary of what I learned. I'm also making this a community wiki in case others want to add to or.. to avoid operator magic by using chain.from_iterable like so chain itertools.chain.from_iterable 1 2 3 5 89 6 print list.. Actually it's less overhead than the question's proposed solution because you throw away the temporary lists you create..

What does `if __name__ == “__main__”:` do?

http://stackoverflow.com/questions/419163/what-does-if-name-main-do

thread.start_new_thread myfunction Thread # 2 2 lock Also what does args mean in this example python idioms python import.. on Harley's answer... When the Python interpreter reads a source file it executes all of the code found in it. Before executing.. if the python interpreter is running that module the source file as the main program it sets the special __name__ variable..

Using global variables in a function other than the one that created them

http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them

print_globvar # Prints 1 I imagine the reason for it is that since global variables are so dangerous Python.. the reason for it is that since global variables are so dangerous Python wants to make sure that you really know that's..

Python's slice notation

http://stackoverflow.com/questions/509211/pythons-slice-notation

through end 1 a # a copy of the whole array There is also the step value which can be used with any of the above a start.. instead of an error. Sometimes you would prefer the error so you have to be aware that this may happen. share improve this..

Making a flat list out of list of lists in Python [duplicate]

http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python

in Python. I can do that in a for loop but maybe there is some cool one liner I tried it with reduce but I get an error... l for item in sublist is faster than the shortcuts posted so far. For evidence as always you can use the timeit module in.. back and forth L 1 times the second I items L 2 times and so on total number of copies is I times the sum of x for x from..

Python: How do I pass a variable by reference?

http://stackoverflow.com/questions/986006/python-how-do-i-pass-a-variable-by-reference

self.variable def Change self var var 'Changed' Is there something I can do to pass the variable by actual reference python.. this question Parameters are passed by value. The reason people are confused by the behaviour is twofold the parameter.. to a variable but the reference is passed by value some data types are mutable but others aren't So If you pass a..

What is a metaclass in Python?

http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python

to do with introspection but it's still unclear to me. So what are metaclasses What do you use them for Concrete examples.. a dictionary to define the attributes of the class. So class Foo object ... bar True Can be translated to Foo type.. type 'type' b.__class__.__class__ type 'type' So a metaclass is just the stuff that creates class objects. You..

Difference between __str__ and __repr__ in Python

http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python

into infinite recursion if objects reference each other . So Python cops out. Note that there is one default which is true..

if x or y or z == blah

http://stackoverflow.com/questions/15112125/if-x-or-y-or-z-blah

if there was a way to translate the first into Python. So say x 0 y 1 z 3 if x or y or z 0 Mylist.append c elif x or y.. on what Python considers false in a boolean context . So for the values x 2 y 1 z 0 x or y or z would resolve to 2 because..

Short Description of Python Scoping Rules

http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules

def spam..... code3 for code4.. code5 x Where is x found Some possible choices include the list above In the enclosing source.. in the built in names module open range SyntaxError ... So in the case of code1 class Foo code2 def spam..... code3 for..

Python “is” operator behaves unexpectedly with integers

http://stackoverflow.com/questions/306313/python-is-operator-behaves-unexpectedly-with-integers

actually just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the..

*args and **kwargs? [duplicate]

http://stackoverflow.com/questions/3394835/args-and-kwargs

star and star do for python parameters 8 answers So I have difficulty with the concept of args and kwargs. So far.. So I have difficulty with the concept of args and kwargs. So far I have learned that args list of arguments as positional..

What kinds of patterns could I enforce on the code to make it easier to translate to another programming language?

http://stackoverflow.com/questions/3455456/what-kinds-of-patterns-could-i-enforce-on-the-code-to-make-it-easier-to-translat

share improve this question I've been building tools DMS Software Reengineering Toolkit to do general purpose program manipulation.. assumptions built in. It has some that give us headaches. So far no black holes. The hardest part of my job over the last..

Flattening a shallow list in Python

http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python

object. But the reduce method is fairly unreadable. So my question is Is there a simple way to flatten this list with..

Python's slice notation

http://stackoverflow.com/questions/509211/pythons-slice-notation

the first value that is not in the selected slice. So the difference beween end and start is the number of elements.. counts from the end of the array instead of the beginning. So a 1 # last item in the array a 2 # last two items in the array.. one element you get an empty list instead of an error. Sometimes you would prefer the error so you have to be aware that..

How can you profile a Python script?

http://stackoverflow.com/questions/582336/how-can-you-profile-a-python-script

asking how to time the execution of their solutions. Sometimes the given answers are somewhat kludgey i.e. adding timing.. little batch file called 'profile.bat' python m cProfile 1 So all I have to do is run profile euler048.py And I get this 1007..

Python: Sort a dictionary by value

http://stackoverflow.com/questions/613183/python-sort-a-dictionary-by-value

Sort a dictionary by value I have a dictionary of values read.. but other types such as lists and tuples are not. So you need a sorted representation which will be a list ”probably..

Making a flat list out of list of lists in Python [duplicate]

http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python

copied over as well as a few new ones added at the end . So for simplicity and without actual loss of generality say you..

Python: How do I pass a variable by reference?

http://stackoverflow.com/questions/986006/python-how-do-i-pass-a-variable-by-reference

by value some data types are mutable but others aren't So If you pass a mutable object into a method the method gets a..

Special (magic) methods in Python [closed]

http://stackoverflow.com/questions/1090620/special-magic-methods-in-python

Google friendly. So I think having a list of those here on SO would be a good idea. python share improve this question..

Attempted relative import in non-package even with __init__.py

http://stackoverflow.com/questions/11536764/attempted-relative-import-in-non-package-even-with-init-py

in non package Searching around I found this and this at SO but not even the accepted answers in those questions work for..

Tabs versus spaces in Python programming

http://stackoverflow.com/questions/119562/tabs-versus-spaces-in-python-programming

programming. But then I came across a question here on SO where someone pointed out that most Python programmers use spaces..

In-memory size of python stucture

http://stackoverflow.com/questions/1331471/in-memory-size-of-python-stucture

64 bit platforms If not this would be nice to have it on SO. The more exhaustive the better So how many bytes are used by..

How to install MySQLdb (Python data access library to MySQL) on Mac OS X?

http://stackoverflow.com/questions/1448429/how-to-install-mysqldb-python-data-access-library-to-mysql-on-mac-os-x

have better experience of the issue... apply a bit of that SO magic. Note Comments in next paragraph applied to Snow Leopard.. it instructions on this fiddly task are available on SO here then download and install the 32 bit version package here..

How get sound input from microphone in python, and process it on the fly?

http://stackoverflow.com/questions/1936828/how-get-sound-input-from-microphone-in-python-and-process-it-on-the-fly

a loud sudden noise or something similar. I searched in SO and found this post http stackoverflow.com questions 1797631..

Python: removing duplicates from a list of lists

http://stackoverflow.com/questions/2213923/python-removing-duplicates-from-a-list-of-lists

question is similar but not quite what I need. Searched SO but didn't find exact duplicate. Benchmarking import itertools..

What are good uses for Python3's “Function Annotations”

http://stackoverflow.com/questions/3038033/what-are-good-uses-for-python3s-function-annotations

implemented in Python3 or any good uses for them. Perhaps SO can enlighten me How it works def foo a 'x' b 5 6 c list max..

Simple wrapping of C code with cython

http://stackoverflow.com/questions/3046305/simple-wrapping-of-c-code-with-cython

from g compile and link. See also distutils doc and or SO questions on distutils . Like make setup.py will rerun cython.. about any SciKit . See also Cython for NumPy users and SO questions tagged cython . To unpack the following files cut..

How to print date in a regular format in Python?

http://stackoverflow.com/questions/311627/how-to-print-date-in-a-regular-format-in-python

Python class inherits object

http://stackoverflow.com/questions/4015417/python-class-inherits-object

docs.python.org release 2.2.3 whatsnew sect rellinks.html SO link for a description of the differences http stackoverflow.com..

What's the proper way to install pip, virtualenv, and distribute for Python?

http://stackoverflow.com/questions/4324558/whats-the-proper-way-to-install-pip-virtualenv-and-distribute-for-python

pip virtualenv and distribute Background In my answer to SO question 4314376 I recommended using ez_setup so that you could.. Glyph's Rebuke In a comment to my answer to SO question 4314376 SO user Glyph stated NO. NEVER EVER do sudo.. Rebuke In a comment to my answer to SO question 4314376 SO user Glyph stated NO. NEVER EVER do sudo python setup.py install..

Python import MySQLdb error - Mac 10.6

http://stackoverflow.com/questions/4730787/python-import-mysqldb-error-mac-10-6

cause of frustration lots of questions about it here on SO and elsewhere. There are a number reasons for that. My advice..

mysql-python installation problems (on mac os x lion)

http://stackoverflow.com/questions/7335853/mysql-python-installation-problems-on-mac-os-x-lion

RELEASE_X86_64 x86_64 I think I have read most SO answers and Google results on the subject can't think of anything..

How do I convert local time to UTC in Python?

http://stackoverflow.com/questions/79797/how-do-i-convert-local-time-to-utc-in-python

time I'm sure I've done this before but can't find it and SO will hopefully help me and others do that in future. Clarification..

Django: Redirect to previous page after login

http://stackoverflow.com/questions/806835/django-redirect-to-previous-page-after-login

with login functionality very similar to the one here on SO. The user should be able to browse the site as an anonymous.. in each request settings.py TEMPLATE_CONTEXT_PROCESSORS django.core.context_processors.auth django.core.context_processors.debug..