title stringlengths 10 172 | question_id int64 469 40.1M | question_body stringlengths 22 48.2k | question_score int64 -44 5.52k | question_date stringlengths 20 20 | answer_id int64 497 40.1M | answer_body stringlengths 18 33.9k | answer_score int64 -38 8.38k | answer_date stringlengths 20 20 | tags list |
|---|---|---|---|---|---|---|---|---|---|
How do I efficiently filter computed values within a Python list comprehension? | 130,262 | <p>The Python list comprehension syntax makes it easy to filter values within a comprehension. For example:</p>
<pre><code>result = [x**2 for x in mylist if type(x) is int]
</code></pre>
<p>Will return a list of the squares of integers in mylist. However, what if the test involves some (costly) computation and you ... | 16 | 2008-09-24T22:08:57Z | 873,661 | <p>I will have a preference for:</p>
<pre><code>itertools.ifilter(bool, (expensive(x) for x in mylist))
</code></pre>
<p>This has the advantage to:</p>
<ul>
<li>avoid None as the function (will be eliminated in Python 3): <a href="http://bugs.python.org/issue2186" rel="nofollow">http://bugs.python.org/issue2186</a><... | 1 | 2009-05-17T01:01:11Z | [
"python",
"list-comprehension"
] |
How do I efficiently filter computed values within a Python list comprehension? | 130,262 | <p>The Python list comprehension syntax makes it easy to filter values within a comprehension. For example:</p>
<pre><code>result = [x**2 for x in mylist if type(x) is int]
</code></pre>
<p>Will return a list of the squares of integers in mylist. However, what if the test involves some (costly) computation and you ... | 16 | 2008-09-24T22:08:57Z | 874,319 | <p>There is the plain old use of a <code>for</code> loop to append to a list, too:</p>
<pre><code>result = []
for x in mylist:
expense = expensive(x)
if expense:
result.append(expense)
</code></pre>
| 0 | 2009-05-17T10:25:40Z | [
"python",
"list-comprehension"
] |
How to pass all Visual Studio 2008 "Macros" to Python script? | 130,587 | <p><em>[NOTE: This questions is similar to but <strong>not the same</strong> as <a href="http://stackoverflow.com/questions/128634/how-to-use-system-environment-variables-in-vs-2008-post-build-events">this one</a>.]</em></p>
<p>Visual Studio defines several dozen "Macros" which are sort of simulated environment variab... | 2 | 2008-09-24T23:28:24Z | 130,773 | <p>This is a bit hacky, but it could work.</p>
<p>Why not call multiple .py scripts in a row?</p>
<p>Each scripts can pass in a small subset of the parameters, and the values to a temp text file. The final script will read and work off of the temp text file.</p>
<p>I agree that this method is filled with danger and ... | 0 | 2008-09-25T00:24:23Z | [
"python",
"visual-studio",
"visual-studio-2008",
"visual-c++",
"visual-studio-2005"
] |
How to pass all Visual Studio 2008 "Macros" to Python script? | 130,587 | <p><em>[NOTE: This questions is similar to but <strong>not the same</strong> as <a href="http://stackoverflow.com/questions/128634/how-to-use-system-environment-variables-in-vs-2008-post-build-events">this one</a>.]</em></p>
<p>Visual Studio defines several dozen "Macros" which are sort of simulated environment variab... | 2 | 2008-09-24T23:28:24Z | 135,078 | <p>You might want to look into PropertySheets. These are files containing Visual C++ settings, including user macros. The sheets can inherit from other sheets and are attached to VC++ projects using the PropertyManager View in Visual Studio. When you create one of these sheets, there is an interface for creating user m... | 2 | 2008-09-25T18:29:30Z | [
"python",
"visual-studio",
"visual-studio-2008",
"visual-c++",
"visual-studio-2005"
] |
How to pass all Visual Studio 2008 "Macros" to Python script? | 130,587 | <p><em>[NOTE: This questions is similar to but <strong>not the same</strong> as <a href="http://stackoverflow.com/questions/128634/how-to-use-system-environment-variables-in-vs-2008-post-build-events">this one</a>.]</em></p>
<p>Visual Studio defines several dozen "Macros" which are sort of simulated environment variab... | 2 | 2008-09-24T23:28:24Z | 4,695,601 | <p>As far as I can tell, the method described in the question is the only way to pass build variables to a Python script.</p>
<p>Perhaps Visual Studio 2010 has something better?</p>
| 0 | 2011-01-14T20:28:15Z | [
"python",
"visual-studio",
"visual-studio-2008",
"visual-c++",
"visual-studio-2005"
] |
Python Date Comparisons | 130,618 | <p>I would like to find out if a particular python datetime object is older than X hours or minutes. I am trying to do something similar to: </p>
<pre><code>if (datetime.now() - self.timestamp) > 100
# Where 100 is either seconds or minutes
</code></pre>
<p>This generates a type error. </p>
<p>What is the proper... | 57 | 2008-09-24T23:34:31Z | 130,623 | <p>You can subtract two <a href="http://docs.python.org/lib/module-datetime.html" rel="nofollow">datetime</a> objects to find the difference between them.<br />
You can use <code>datetime.fromtimestamp</code> to parse a POSIX time stamp.</p>
| 0 | 2008-09-24T23:35:26Z | [
"python",
"datetime"
] |
Python Date Comparisons | 130,618 | <p>I would like to find out if a particular python datetime object is older than X hours or minutes. I am trying to do something similar to: </p>
<pre><code>if (datetime.now() - self.timestamp) > 100
# Where 100 is either seconds or minutes
</code></pre>
<p>This generates a type error. </p>
<p>What is the proper... | 57 | 2008-09-24T23:34:31Z | 130,647 | <p>You can use a combination of the 'days' and 'seconds' attributes of the returned object to figure out the answer, like this:</p>
<pre><code>def seconds_difference(stamp1, stamp2):
delta = stamp1 - stamp2
return 24*60*60*delta.days + delta.seconds + delta.microseconds/1000000.
</code></pre>
<p>Use abs() in ... | 1 | 2008-09-24T23:40:43Z | [
"python",
"datetime"
] |
Python Date Comparisons | 130,618 | <p>I would like to find out if a particular python datetime object is older than X hours or minutes. I am trying to do something similar to: </p>
<pre><code>if (datetime.now() - self.timestamp) > 100
# Where 100 is either seconds or minutes
</code></pre>
<p>This generates a type error. </p>
<p>What is the proper... | 57 | 2008-09-24T23:34:31Z | 130,652 | <p>Compare the difference to a timedelta that you create:</p>
<pre><code>if datetime.datetime.now() - timestamp > datetime.timedelta(seconds = 5):
print 'older'
</code></pre>
| 5 | 2008-09-24T23:42:01Z | [
"python",
"datetime"
] |
Python Date Comparisons | 130,618 | <p>I would like to find out if a particular python datetime object is older than X hours or minutes. I am trying to do something similar to: </p>
<pre><code>if (datetime.now() - self.timestamp) > 100
# Where 100 is either seconds or minutes
</code></pre>
<p>This generates a type error. </p>
<p>What is the proper... | 57 | 2008-09-24T23:34:31Z | 130,665 | <p>Use the <code>datetime.timedelta</code> class:</p>
<pre><code>>>> from datetime import datetime, timedelta
>>> then = datetime.now() - timedelta(hours = 2)
>>> now = datetime.now()
>>> (now - then) > timedelta(days = 1)
False
>>> (now - then) > timedelta(hours = 1)... | 102 | 2008-09-24T23:44:27Z | [
"python",
"datetime"
] |
Python Date Comparisons | 130,618 | <p>I would like to find out if a particular python datetime object is older than X hours or minutes. I am trying to do something similar to: </p>
<pre><code>if (datetime.now() - self.timestamp) > 100
# Where 100 is either seconds or minutes
</code></pre>
<p>This generates a type error. </p>
<p>What is the proper... | 57 | 2008-09-24T23:34:31Z | 130,669 | <p>Like so:</p>
<pre><code># self.timestamp should be a datetime object
if (datetime.now() - self.timestamp).seconds > 100:
print "object is over 100 seconds old"
</code></pre>
| 0 | 2008-09-24T23:45:28Z | [
"python",
"datetime"
] |
Python Date Comparisons | 130,618 | <p>I would like to find out if a particular python datetime object is older than X hours or minutes. I am trying to do something similar to: </p>
<pre><code>if (datetime.now() - self.timestamp) > 100
# Where 100 is either seconds or minutes
</code></pre>
<p>This generates a type error. </p>
<p>What is the proper... | 57 | 2008-09-24T23:34:31Z | 10,546,316 | <p>Alternative:</p>
<pre><code>if (datetime.now() - self.timestamp).total_seconds() > 100:
</code></pre>
<p>Assuming self.timestamp is an datetime instance</p>
| 5 | 2012-05-11T06:14:38Z | [
"python",
"datetime"
] |
Request UAC elevation from within a Python script? | 130,763 | <p>I want my Python script to copy files on Vista. When I run it from a normal <code>cmd.exe</code> window, no errors are generated, yet the files are NOT copied. If I run <code>cmd.exe</code> "as administator" and then run my script, it works fine.</p>
<p>This makes sense since User Account Control (UAC) normally pre... | 49 | 2008-09-25T00:22:26Z | 131,092 | <p>It seems there's no way to elevate the application privileges for a while for you to perform a particular task. Windows needs to know at the start of the program whether the application requires certain privileges, and will ask the user to confirm when the application performs any tasks that <em>need</em> those priv... | 28 | 2008-09-25T02:01:34Z | [
"python",
"windows",
"windows-vista",
"uac"
] |
Request UAC elevation from within a Python script? | 130,763 | <p>I want my Python script to copy files on Vista. When I run it from a normal <code>cmd.exe</code> window, no errors are generated, yet the files are NOT copied. If I run <code>cmd.exe</code> "as administator" and then run my script, it works fine.</p>
<p>This makes sense since User Account Control (UAC) normally pre... | 49 | 2008-09-25T00:22:26Z | 138,970 | <p>If your script always requires an Administrator's privileges then: </p>
<pre><code>runas /user:Administrator "python your_script.py"
</code></pre>
| 0 | 2008-09-26T11:54:18Z | [
"python",
"windows",
"windows-vista",
"uac"
] |
Request UAC elevation from within a Python script? | 130,763 | <p>I want my Python script to copy files on Vista. When I run it from a normal <code>cmd.exe</code> window, no errors are generated, yet the files are NOT copied. If I run <code>cmd.exe</code> "as administator" and then run my script, it works fine.</p>
<p>This makes sense since User Account Control (UAC) normally pre... | 49 | 2008-09-25T00:22:26Z | 3,787,689 | <p>This may not completely answer your question but you could also try using the Elevate Command Powertoy in order to run the script with elevated UAC privileges.</p>
<p><a href="http://technet.microsoft.com/en-us/magazine/2008.06.elevation.aspx" rel="nofollow">http://technet.microsoft.com/en-us/magazine/2008.06.eleva... | 2 | 2010-09-24T13:43:33Z | [
"python",
"windows",
"windows-vista",
"uac"
] |
Request UAC elevation from within a Python script? | 130,763 | <p>I want my Python script to copy files on Vista. When I run it from a normal <code>cmd.exe</code> window, no errors are generated, yet the files are NOT copied. If I run <code>cmd.exe</code> "as administator" and then run my script, it works fine.</p>
<p>This makes sense since User Account Control (UAC) normally pre... | 49 | 2008-09-25T00:22:26Z | 11,746,382 | <p>It took me a little while to get dguaraglia's answer working, so in the interest of saving others time, here's what I did to implement this idea:</p>
<pre><code>import os
import sys
import win32com.shell.shell as shell
ASADMIN = 'asadmin'
if sys.argv[-1] != ASADMIN:
script = os.path.abspath(sys.argv[0])
pa... | 49 | 2012-07-31T18:09:35Z | [
"python",
"windows",
"windows-vista",
"uac"
] |
Request UAC elevation from within a Python script? | 130,763 | <p>I want my Python script to copy files on Vista. When I run it from a normal <code>cmd.exe</code> window, no errors are generated, yet the files are NOT copied. If I run <code>cmd.exe</code> "as administator" and then run my script, it works fine.</p>
<p>This makes sense since User Account Control (UAC) normally pre... | 49 | 2008-09-25T00:22:26Z | 22,821,704 | <p>You can make a shortcut somewhere and as the target use:
python yourscript.py
then under properties and advanced select run as administrator. </p>
<p>When the user executes the shortcut it will ask them to elevate the application.</p>
| 1 | 2014-04-02T19:56:40Z | [
"python",
"windows",
"windows-vista",
"uac"
] |
Request UAC elevation from within a Python script? | 130,763 | <p>I want my Python script to copy files on Vista. When I run it from a normal <code>cmd.exe</code> window, no errors are generated, yet the files are NOT copied. If I run <code>cmd.exe</code> "as administator" and then run my script, it works fine.</p>
<p>This makes sense since User Account Control (UAC) normally pre... | 49 | 2008-09-25T00:22:26Z | 32,230,199 | <p>Recognizing this question was asked years ago, I think a more elegant solution is offered on <a href="https://github.com/frmdstryr/pywinutils" rel="nofollow">github</a> by frmdstryr using his module pyminutils: </p>
<p>Excerpt:</p>
<pre><code>import pythoncom
from win32com.shell import shell,shellcon
def copy(src... | 2 | 2015-08-26T15:03:36Z | [
"python",
"windows",
"windows-vista",
"uac"
] |
Request UAC elevation from within a Python script? | 130,763 | <p>I want my Python script to copy files on Vista. When I run it from a normal <code>cmd.exe</code> window, no errors are generated, yet the files are NOT copied. If I run <code>cmd.exe</code> "as administator" and then run my script, it works fine.</p>
<p>This makes sense since User Account Control (UAC) normally pre... | 49 | 2008-09-25T00:22:26Z | 34,216,774 | <p>A variation on Jorenko's work above allows the elevated process to use the same console (but see my comment below):</p>
<pre><code>def spawn_as_administrator():
""" Spawn ourself with administrator rights and wait for new process to exit
Make the new process use the same console as the old one.
... | 1 | 2015-12-11T05:34:25Z | [
"python",
"windows",
"windows-vista",
"uac"
] |
Style - When to serialize a Django model Instance: signals vs model's save method | 131,327 | <p>I plan to serialize a Django model to XML when it's saved or updated. (The XML's going to be imported into a flash movie). Is it better to listen for a post_save() or pre_save() signal and then perform the serialization, or to just handle it in the model's save() methon</p>
| 1 | 2008-09-25T03:25:52Z | 131,383 | <p>Post save. That way the new data (the reason for performing the serialization) is already in the database. It'll make for a much cleaner bit of code that simply takes from the database and doesn't have to worry about adding an extra value.</p>
<p>The other way that comes to mind is to maintain the xml file in paral... | 0 | 2008-09-25T03:43:34Z | [
"python",
"django"
] |
Style - When to serialize a Django model Instance: signals vs model's save method | 131,327 | <p>I plan to serialize a Django model to XML when it's saved or updated. (The XML's going to be imported into a flash movie). Is it better to listen for a post_save() or pre_save() signal and then perform the serialization, or to just handle it in the model's save() methon</p>
| 1 | 2008-09-25T03:25:52Z | 136,399 | <p>If it's core functionality for saving the model you'll want it as part of the save method. However, if you already have a functioning model and you want to extend it for other purposes then signals are your best bet since they allow for properly decoupled modules.</p>
<p>A good example might be that you want to add... | 2 | 2008-09-25T21:49:44Z | [
"python",
"django"
] |
Do I have to cause an ValueError in Python | 131,449 | <p>I have this code:</p>
<pre><code>chars = #some list
try:
indx = chars.index(chars)
except ValueError:
#doSomething
else:
#doSomethingElse
</code></pre>
<p>I want to be able to do this because I don't like knowfully causing Exceptions:</p>
<pre><code>chars = #some list
indx = chars.index(chars)
if in... | 3 | 2008-09-25T04:14:34Z | 131,452 | <pre><code>if element in mylist:
index = mylist.index(element)
# ... do something
else:
# ... do something else
</code></pre>
| 7 | 2008-09-25T04:16:25Z | [
"python",
"exception",
"list"
] |
Do I have to cause an ValueError in Python | 131,449 | <p>I have this code:</p>
<pre><code>chars = #some list
try:
indx = chars.index(chars)
except ValueError:
#doSomething
else:
#doSomethingElse
</code></pre>
<p>I want to be able to do this because I don't like knowfully causing Exceptions:</p>
<pre><code>chars = #some list
indx = chars.index(chars)
if in... | 3 | 2008-09-25T04:14:34Z | 131,522 | <p>Note that the latter approach is going against the generally accepted "pythonic" philosophy of <a href="http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Exceptions" rel="nofollow">EAFP, or "It is Easier to Ask for Forgiveness than Permission."</a>, while the former follows it.</p>
| 9 | 2008-09-25T04:47:35Z | [
"python",
"exception",
"list"
] |
Do I have to cause an ValueError in Python | 131,449 | <p>I have this code:</p>
<pre><code>chars = #some list
try:
indx = chars.index(chars)
except ValueError:
#doSomething
else:
#doSomethingElse
</code></pre>
<p>I want to be able to do this because I don't like knowfully causing Exceptions:</p>
<pre><code>chars = #some list
indx = chars.index(chars)
if in... | 3 | 2008-09-25T04:14:34Z | 131,930 | <p>For the specific case where your list is a sequence of single-character strings you can get what you want by changing the list to be searched to a string in advance (eg. ''.join(chars)).</p>
<p>You can then use the .find() method, which does work as you want. However, there's no corresponding method for lists or t... | 0 | 2008-09-25T07:27:39Z | [
"python",
"exception",
"list"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 132,108 | <p><em>python -dv yourscript.py</em></p>
<p>That will make the interpreter to run in debug mode and to give you a trace of what the interpreter is doing.</p>
<p>If you want to interactively debug the code you should run it like this:</p>
<p><em>python -m pdb yourscript.py</em></p>
<p>That tells the python interpret... | 10 | 2008-09-25T08:24:24Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 132,114 | <p>The <a href="http://www.python.org/doc/2.5.2/lib/module-traceback.html"><strong>traceback</strong></a> module has some nice functions, among them: print_stack:</p>
<pre><code>import traceback
traceback.print_stack()
</code></pre>
| 24 | 2008-09-25T08:27:16Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 132,123 | <pre><code>>>> import traceback
>>> def x():
>>> print traceback.extract_stack()
>>> x()
[('<stdin>', 1, '<module>', None), ('<stdin>', 2, 'x', None)]
</code></pre>
<p>You can also nicely format the stack trace, see the <a href="http://docs.python.org/lib/modul... | 31 | 2008-09-25T08:29:32Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 132,260 | <p>I don't know of anything similar to <a href="http://java.sun.com/developer/onlineTraining/Programming/JDCBook/stack.html" rel="nofollow">java's response to SIGQUIT</a>, so you might have to build it in to your application. Maybe you could make a server in another thread that can get a stacktrace on response to a mes... | 0 | 2008-09-25T09:06:39Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 132,268 | <p>There is no way to hook into a running python process and get reasonable results. What I do if processes lock up is hooking strace in and trying to figure out what exactly is happening.</p>
<p>Unfortunately often strace is the observer that "fixes" race conditions so that the output is useless there too.</p>
| 1 | 2008-09-25T09:09:54Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 133,384 | <p>I have module I use for situations like this - where a process will be running for a long time but gets stuck sometimes for unknown and irreproducible reasons. Its a bit hacky, and only works on unix (requires signals):</p>
<pre><code>import code, traceback, signal
def debug(sig, frame):
"""Interrupt running ... | 251 | 2008-09-25T13:38:45Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 147,114 | <p>The suggestion to install a signal handler is a good one, and I use it a lot. For example, <a href="http://bazaar-vcs.org/">bzr</a> by default installs a SIGQUIT handler that invokes <code>pdb.set_trace()</code> to immediately drop you into a <a href="http://docs.python.org/lib/module-pdb.html">pdb</a> prompt. (Se... | 118 | 2008-09-29T00:44:13Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 490,172 | <p>It's worth looking at <a href="http://bashdb.sourceforge.net/pydb/" rel="nofollow">Pydb</a>, "an expanded version of the Python debugger loosely based on the gdb command set". It includes signal managers which can take care of starting the debugger when a specified signal is sent.</p>
<p>A 2006 Summer of Code proje... | 3 | 2009-01-29T01:28:29Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 618,748 | <p>What really helped me here is <a href="http://stackoverflow.com/questions/132058/getting-stack-trace-from-a-running-python-application/147114#147114">spiv's tip</a> (which I would vote up and comment on if I had the reputation points) for getting a stack trace out of an <em>unprepared</em> Python process. Except it ... | 17 | 2009-03-06T12:49:11Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 1,431,341 | <p>use the inspect module.</p>
<blockquote>
<blockquote>
<blockquote>
<p>import inspect
help(inspect.stack)
Help on function stack in module inspect:</p>
</blockquote>
</blockquote>
</blockquote>
<p>stack(context=1)
Return a list of records for the stack above the caller's frame.</p>... | 0 | 2009-09-16T06:44:42Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 2,569,696 | <p>I am almost always dealing with multiple threads and main thread is generally not doing much, so what is most interesting is to dump all the stacks (which is more like the Java's dump). Here is an implementation based on <a href="http://bzimmer.ziclix.com/2008/12/17/python-thread-dumps/">this blog</a>:</p>
<pre><co... | 56 | 2010-04-02T23:23:47Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 5,503,185 | <p>I would add this as a comment to <a href="http://stackoverflow.com/questions/132058/getting-stack-trace-from-a-running-python-application/2569696#2569696">haridsv's response</a>, but I lack the reputation to do so:</p>
<p>Some of us are still stuck on a version of Python older than 2.6 (required for Thread.ident), ... | 9 | 2011-03-31T16:30:51Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 7,224,091 | <p>On Solaris, you can use pstack(1) No changes to the python code are necessary. eg.</p>
<pre><code># pstack 16000 | grep : | head
16000: /usr/bin/python2.6 /usr/lib/pkg.depotd --cfg svc:/application/pkg/serv
[ /usr/lib/python2.6/vendor-packages/cherrypy/process/wspbus.py:282 (_wait) ]
[ /usr/lib/python2.6/vendor-pac... | 6 | 2011-08-28T21:30:38Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 9,019,164 | <p>Take a look at the <a href="http://docs.python.org/3.3/whatsnew/3.3.html#faulthandler" rel="nofollow"><code>faulthandler</code></a> module, new in Python 3.3. A <a href="https://pypi.python.org/pypi/faulthandler/" rel="nofollow"><code>faulthandler</code> backport</a> for use in Python 2 is available on PyPI.</p>
| 7 | 2012-01-26T13:57:38Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 10,047,855 | <p>I hacked together some tool which attaches into a running Python process and injects some code to get a Python shell.</p>
<p>See here: <a href="https://github.com/albertz/pydbattach" rel="nofollow">https://github.com/albertz/pydbattach</a></p>
| 3 | 2012-04-06T18:49:30Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 10,165,776 | <p>I was looking for a while for a solution to debug my threads and I found it here thanks to haridsv. I use slightly simplified version employing the traceback.print_stack():</p>
<pre><code>import sys, traceback, signal
import threading
import os
def dumpstacks(signal, frame):
id2name = dict((th.ident, th.name) fo... | 3 | 2012-04-15T20:28:10Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 16,246,063 | <p>You can try the <a href="http://docs.python.org/dev/library/faulthandler.html">faulthandler module</a>. Install it using <code>pip install faulthandler</code> and add:</p>
<pre><code>import faulthandler, signal
faulthandler.register(signal.SIGUSR1)
</code></pre>
<p>at the beginning of your program. Then send SIGUS... | 14 | 2013-04-26T22:18:58Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 16,247,213 | <p>You can use <a href="https://pypi.python.org/pypi/pudb" rel="nofollow">PuDB</a>, a Python debugger with a curses interface to do this. Just add </p>
<pre><code>from pudb import set_interrupt_handler; set_interrupt_handler()
</code></pre>
<p>to your code and use Ctrl-C when you want to break. You can continue with ... | 1 | 2013-04-27T00:54:57Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 17,270,641 | <p>If you're on a Linux system, use the awesomeness of <code>gdb</code> with Python debug extensions (can be in <code>python-dbg</code> or <code>python-debuginfo</code> package). It also helps with multithreaded applications, GUI applications and C modules.</p>
<p>Run your program with:</p>
<pre><code>$ gdb -ex r --a... | 5 | 2013-06-24T08:03:31Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 23,405,280 | <p><a href="https://github.com/google/pyringe" rel="nofollow">pyringe</a> is a debugger that can interact with running python processes, print stack traces, variables, etc. without any a priori setup.</p>
<p>While I've often used the signal handler solution in the past, it can still often be difficult to reproduce the... | 2 | 2014-05-01T09:40:07Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 27,465,699 | <p>In Python 3, pdb will automatically install a signal handler the first time you use c(ont(inue)) in the debugger. Pressing Control-C afterwards will drop you right back in there. In Python 2, here's a one-liner which should work even in relatively old versions (tested in 2.7 but I checked Python source back to 2.4 a... | 0 | 2014-12-14T02:16:25Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 29,881,630 | <p>Getting a stack trace of an <em>unprepared</em> python program, running in a stock python <em>without debugging symbols</em> can be done with <a href="http://pyrasite.readthedocs.org/">pyrasite</a>. Worked like a charm for me in on Ubuntu Trusty:</p>
<pre><code>$ sudo pip install pyrasite
$ echo 0 | sudo tee /proc/... | 16 | 2015-04-26T18:22:20Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Showing the stack trace from a running Python application | 132,058 | <p>I have this Python application that gets stuck from time to time and I can't find out where.</p>
<p>Is there any way to signal Python interpreter to show you the exact code that's running? </p>
<p>Some kind of on-the-fly stacktrace?</p>
<p><strong><em>Related questions:</em></strong></p>
<ul>
<li><a href="http:/... | 250 | 2008-09-25T08:06:06Z | 29,911,672 | <p>In case you need to do this with uWSGI, it has <a href="http://uwsgi-docs.readthedocs.org/en/latest/Tracebacker.html" rel="nofollow">Python Tracebacker</a> built-in and it's just matter of enabling it in the configuration (number is attached to the name for each worker):</p>
<pre><code>py-tracebacker=/var/run/uwsgi... | 0 | 2015-04-28T06:15:05Z | [
"python",
"debugging",
"stack-trace",
"traceback"
] |
Translate algorithmic C to Python | 132,411 | <p>I would like to translate some C code to Python code or bytecode. The C code in question is what i'd call purely algorithmic: platform independent, no I/O, just algorithms and in-memory data structures.</p>
<p>An example would be a regular expression library. Translation tool would process library source code and p... | 8 | 2008-09-25T09:56:43Z | 132,425 | <p>Why not keeping the C code and <a href="http://www.python.org/doc/ext/intro.html" rel="nofollow">creating a Python C module</a> which can be imported into a running Python environment?</p>
| 0 | 2008-09-25T10:00:14Z | [
"java",
"python",
"c",
"sandbox",
"code-translation"
] |
Translate algorithmic C to Python | 132,411 | <p>I would like to translate some C code to Python code or bytecode. The C code in question is what i'd call purely algorithmic: platform independent, no I/O, just algorithms and in-memory data structures.</p>
<p>An example would be a regular expression library. Translation tool would process library source code and p... | 8 | 2008-09-25T09:56:43Z | 132,438 | <p>First, i'd consider wrapping the existing C library with Pythonic goodness to provide an API in the form of a python module. I'd look at swig, ctypes, pyrex, and whatever else is out there these days. The C library itself would stay there unchanged. Saves work. </p>
<p>But if i really had to write original Pyt... | 0 | 2008-09-25T10:07:47Z | [
"java",
"python",
"c",
"sandbox",
"code-translation"
] |
Translate algorithmic C to Python | 132,411 | <p>I would like to translate some C code to Python code or bytecode. The C code in question is what i'd call purely algorithmic: platform independent, no I/O, just algorithms and in-memory data structures.</p>
<p>An example would be a regular expression library. Translation tool would process library source code and p... | 8 | 2008-09-25T09:56:43Z | 132,459 | <p>There is frankly no way to mechanically and meaningfully translate C to Python without suffering an insane performance penalty. As we all know Python isn't anywhere near C speed (with current compilers and interpreters) but worse than that is that what C is good at (bit-fiddling, integer math, tricks with blocks of ... | 12 | 2008-09-25T10:12:54Z | [
"java",
"python",
"c",
"sandbox",
"code-translation"
] |
Translate algorithmic C to Python | 132,411 | <p>I would like to translate some C code to Python code or bytecode. The C code in question is what i'd call purely algorithmic: platform independent, no I/O, just algorithms and in-memory data structures.</p>
<p>An example would be a regular expression library. Translation tool would process library source code and p... | 8 | 2008-09-25T09:56:43Z | 132,470 | <p>Any automatic translation is going to suffer for not using the power of Python. C-type procedural code would run very slowly if translated directly into Python, you would need to profile and replace whole sections with more Python-optimized code.</p>
| 0 | 2008-09-25T10:16:49Z | [
"java",
"python",
"c",
"sandbox",
"code-translation"
] |
Translate algorithmic C to Python | 132,411 | <p>I would like to translate some C code to Python code or bytecode. The C code in question is what i'd call purely algorithmic: platform independent, no I/O, just algorithms and in-memory data structures.</p>
<p>An example would be a regular expression library. Translation tool would process library source code and p... | 8 | 2008-09-25T09:56:43Z | 132,493 | <p>The fastest way (in terms of programmer effort, not efficiency) would probably involve using an existing compiler to compile C to something simple (for example LLVM) and either:</p>
<ul>
<li>interpret that in Python (exorbitant performance penalty)</li>
<li>translate that to Python (huge performance penalty)</li>
<... | 3 | 2008-09-25T10:25:45Z | [
"java",
"python",
"c",
"sandbox",
"code-translation"
] |
Translate algorithmic C to Python | 132,411 | <p>I would like to translate some C code to Python code or bytecode. The C code in question is what i'd call purely algorithmic: platform independent, no I/O, just algorithms and in-memory data structures.</p>
<p>An example would be a regular expression library. Translation tool would process library source code and p... | 8 | 2008-09-25T09:56:43Z | 135,209 | <p>You can always compile the C code, and load in the libraries using ctypes in python.</p>
| -1 | 2008-09-25T18:52:54Z | [
"java",
"python",
"c",
"sandbox",
"code-translation"
] |
Translate algorithmic C to Python | 132,411 | <p>I would like to translate some C code to Python code or bytecode. The C code in question is what i'd call purely algorithmic: platform independent, no I/O, just algorithms and in-memory data structures.</p>
<p>An example would be a regular expression library. Translation tool would process library source code and p... | 8 | 2008-09-25T09:56:43Z | 135,837 | <p>I'd personnaly use a tool to extract an uml sheme from the C code, then use it to generate python code.</p>
<p>From this squeleton, I's start to get rid of the uncessary C-style structures and then I'd fill the methods with python code.</p>
<p>I think it would be the safer and yet most efficient way.</p>
| 0 | 2008-09-25T20:29:33Z | [
"java",
"python",
"c",
"sandbox",
"code-translation"
] |
Translate algorithmic C to Python | 132,411 | <p>I would like to translate some C code to Python code or bytecode. The C code in question is what i'd call purely algorithmic: platform independent, no I/O, just algorithms and in-memory data structures.</p>
<p>An example would be a regular expression library. Translation tool would process library source code and p... | 8 | 2008-09-25T09:56:43Z | 782,839 | <p>Write a C interpreter in pure Python? ;-)</p>
| 1 | 2009-04-23T17:51:26Z | [
"java",
"python",
"c",
"sandbox",
"code-translation"
] |
Translate algorithmic C to Python | 132,411 | <p>I would like to translate some C code to Python code or bytecode. The C code in question is what i'd call purely algorithmic: platform independent, no I/O, just algorithms and in-memory data structures.</p>
<p>An example would be a regular expression library. Translation tool would process library source code and p... | 8 | 2008-09-25T09:56:43Z | 6,857,972 | <p>use indent(1) and ctopy(1)... for extra credit test speeds on pypy... for bonus credit use pyastra to generate assembly code.</p>
<p>Regardless of language you will always have to sacrifice storing outputs of various constructs and functions between run-time space (CPU) or memory-space (RAM).</p>
<p>Check the grea... | 4 | 2011-07-28T11:09:45Z | [
"java",
"python",
"c",
"sandbox",
"code-translation"
] |
Regex to remove conditional comments | 132,488 | <p>I want a regex which can match conditional comments in a HTML source page so I can remove only those. I want to preserve the regular comments.</p>
<p>I would also like to avoid using the .*? notation if possible. </p>
<p>The text is </p>
<pre><code>foo
<!--[if IE]>
<style type="text/css">
ul.menu u... | 5 | 2008-09-25T10:23:33Z | 132,499 | <p>Don't use a regular expression for this. You will get confused about comments containing opening tags and what not, and do the wrong thing. HTML isn't regular, and trying to modify it with a single regular expression will fail.</p>
<p>Use a HTML parser for this. BeautifulSoup is a good, easy, flexible and sturdy on... | 0 | 2008-09-25T10:27:06Z | [
"python",
"regex"
] |
Regex to remove conditional comments | 132,488 | <p>I want a regex which can match conditional comments in a HTML source page so I can remove only those. I want to preserve the regular comments.</p>
<p>I would also like to avoid using the .*? notation if possible. </p>
<p>The text is </p>
<pre><code>foo
<!--[if IE]>
<style type="text/css">
ul.menu u... | 5 | 2008-09-25T10:23:33Z | 132,519 | <p>@<a href="#132503" rel="nofollow">Benoit </a></p>
<p>Small Correction (with multiline turned on): </p>
<pre><code> "<!--\[if IE\]>.*?<!\[endif\]-->"
</code></pre>
| 1 | 2008-09-25T10:31:59Z | [
"python",
"regex"
] |
Regex to remove conditional comments | 132,488 | <p>I want a regex which can match conditional comments in a HTML source page so I can remove only those. I want to preserve the regular comments.</p>
<p>I would also like to avoid using the .*? notation if possible. </p>
<p>The text is </p>
<pre><code>foo
<!--[if IE]>
<style type="text/css">
ul.menu u... | 5 | 2008-09-25T10:23:33Z | 132,521 | <p>This works in Visual Studio 2005, where there is no line span option:</p>
<p><code>\<!--\[if IE\]\>{.|\n}*\<!\[endif\]--\></code></p>
| 0 | 2008-09-25T10:32:39Z | [
"python",
"regex"
] |
Regex to remove conditional comments | 132,488 | <p>I want a regex which can match conditional comments in a HTML source page so I can remove only those. I want to preserve the regular comments.</p>
<p>I would also like to avoid using the .*? notation if possible. </p>
<p>The text is </p>
<pre><code>foo
<!--[if IE]>
<style type="text/css">
ul.menu u... | 5 | 2008-09-25T10:23:33Z | 132,532 | <pre><code>>>> from BeautifulSoup import BeautifulSoup, Comment
>>> html = '<html><!--[if IE]> bloo blee<![endif]--></html>'
>>> soup = BeautifulSoup(html)
>>> comments = soup.findAll(text=lambda text:isinstance(text, Comment)
and text.find('if... | 4 | 2008-09-25T10:34:45Z | [
"python",
"regex"
] |
Regex to remove conditional comments | 132,488 | <p>I want a regex which can match conditional comments in a HTML source page so I can remove only those. I want to preserve the regular comments.</p>
<p>I would also like to avoid using the .*? notation if possible. </p>
<p>The text is </p>
<pre><code>foo
<!--[if IE]>
<style type="text/css">
ul.menu u... | 5 | 2008-09-25T10:23:33Z | 132,561 | <p>Here's what you'll need:</p>
<pre><code><!(|--)\[[^\]]+\]>.+?<!\[endif\](|--)>
</code></pre>
<p>It will filter out all sorts of conditional comments including:</p>
<pre><code><!--[if anything]>
...
<[endif]-->
</code></pre>
<p>and</p>
<pre><code><![if ! IE 6]>
...
<![end... | 1 | 2008-09-25T10:46:16Z | [
"python",
"regex"
] |
Regex to remove conditional comments | 132,488 | <p>I want a regex which can match conditional comments in a HTML source page so I can remove only those. I want to preserve the regular comments.</p>
<p>I would also like to avoid using the .*? notation if possible. </p>
<p>The text is </p>
<pre><code>foo
<!--[if IE]>
<style type="text/css">
ul.menu u... | 5 | 2008-09-25T10:23:33Z | 135,916 | <p>I'd simply go with :</p>
<pre><code>import re
html = """fjlk<wb>dsqfjqdsmlkf fdsijfmldsqjfl fjdslmfkqsjf<---- fdjslmjkqfs---><!--[if lt IE 7.]>\
<script defer type="text/javascript" src="pngfix_253168.js"></script><!--png fix for IE-->\
<![endif]-->fjlk<wb>dsqfjqdsml... | 1 | 2008-09-25T20:42:26Z | [
"python",
"regex"
] |
Regex to remove conditional comments | 132,488 | <p>I want a regex which can match conditional comments in a HTML source page so I can remove only those. I want to preserve the regular comments.</p>
<p>I would also like to avoid using the .*? notation if possible. </p>
<p>The text is </p>
<pre><code>foo
<!--[if IE]>
<style type="text/css">
ul.menu u... | 5 | 2008-09-25T10:23:33Z | 137,831 | <p>As I see it, you only need to worry about <em>downlevel-hidden</em> comments (the ones that start with <code><!--</code>), and you don't need to match anything beyond the word <code>if</code> and the space following it. This should do what you want:</p>
<pre><code>"<!--\[if\s(?:[^<]+|<(?!!\[endif\]--&g... | 1 | 2008-09-26T04:51:12Z | [
"python",
"regex"
] |
Is there a difference between `==` and `is` in Python? | 132,988 | <p>My <a href="http://en.wiktionary.org/wiki/Google-fu">Google-fu</a> has failed me.</p>
<p>In Python, are the following two tests for equality equivalent (ha!)?</p>
<pre><code>n = 5
# Test one.
if n == 5:
print 'Yay!'
# Test two.
if n is 5:
print 'Yay!'
</code></pre>
<p>Does this hold true for objects wher... | 299 | 2008-09-25T12:27:09Z | 133,017 | <p>== determines if the values are equivalent, while "is" determines if they are the exact same object. </p>
| 13 | 2008-09-25T12:31:12Z | [
"python",
"reference",
"semantics"
] |
Is there a difference between `==` and `is` in Python? | 132,988 | <p>My <a href="http://en.wiktionary.org/wiki/Google-fu">Google-fu</a> has failed me.</p>
<p>In Python, are the following two tests for equality equivalent (ha!)?</p>
<pre><code>n = 5
# Test one.
if n == 5:
print 'Yay!'
# Test two.
if n is 5:
print 'Yay!'
</code></pre>
<p>Does this hold true for objects wher... | 299 | 2008-09-25T12:27:09Z | 133,022 | <p><a href="https://docs.python.org/library/stdtypes.html#comparisons" rel="nofollow">https://docs.python.org/library/stdtypes.html#comparisons</a></p>
<p><code>is</code> tests for identity
<code>==</code> tests for equality</p>
<p>Each (small) integer value is mapped to a single value, so every 3 is identical and eq... | 5 | 2008-09-25T12:31:57Z | [
"python",
"reference",
"semantics"
] |
Is there a difference between `==` and `is` in Python? | 132,988 | <p>My <a href="http://en.wiktionary.org/wiki/Google-fu">Google-fu</a> has failed me.</p>
<p>In Python, are the following two tests for equality equivalent (ha!)?</p>
<pre><code>n = 5
# Test one.
if n == 5:
print 'Yay!'
# Test two.
if n is 5:
print 'Yay!'
</code></pre>
<p>Does this hold true for objects wher... | 299 | 2008-09-25T12:27:09Z | 133,024 | <p><code>is</code> will return <code>True</code> if two variables point to the same object, <code>==</code> if the objects referred to by the variables are equal.</p>
<pre><code>>>> a = [1, 2, 3]
>>> b = a
>>> b is a
True
>>> b == a
True
>>> b = a[:]
>>> b is a
Fa... | 439 | 2008-09-25T12:32:37Z | [
"python",
"reference",
"semantics"
] |
Is there a difference between `==` and `is` in Python? | 132,988 | <p>My <a href="http://en.wiktionary.org/wiki/Google-fu">Google-fu</a> has failed me.</p>
<p>In Python, are the following two tests for equality equivalent (ha!)?</p>
<pre><code>n = 5
# Test one.
if n == 5:
print 'Yay!'
# Test two.
if n is 5:
print 'Yay!'
</code></pre>
<p>Does this hold true for objects wher... | 299 | 2008-09-25T12:27:09Z | 133,035 | <p>Your answer is correct. The <code>is</code> operator compares the identity of two objects. The <code>==</code> operator compares the values of two objects.</p>
<p>An object's identity never changes once it has been created; you may think of it as the object's address in memory.</p>
<p>You can control comparison b... | 5 | 2008-09-25T12:34:18Z | [
"python",
"reference",
"semantics"
] |
Is there a difference between `==` and `is` in Python? | 132,988 | <p>My <a href="http://en.wiktionary.org/wiki/Google-fu">Google-fu</a> has failed me.</p>
<p>In Python, are the following two tests for equality equivalent (ha!)?</p>
<pre><code>n = 5
# Test one.
if n == 5:
print 'Yay!'
# Test two.
if n is 5:
print 'Yay!'
</code></pre>
<p>Does this hold true for objects wher... | 299 | 2008-09-25T12:27:09Z | 134,631 | <p>They are <b>completely different</b>. <code>is</code> checks for object identity, while <code>==</code> checks for equality (a notion that depends on the two operands' types).</p>
<p>It is only a lucky coincidence that "<code>is</code>" seems to work correctly with small integers (e.g. 5 == 4+1). That is because ... | 8 | 2008-09-25T17:15:38Z | [
"python",
"reference",
"semantics"
] |
Is there a difference between `==` and `is` in Python? | 132,988 | <p>My <a href="http://en.wiktionary.org/wiki/Google-fu">Google-fu</a> has failed me.</p>
<p>In Python, are the following two tests for equality equivalent (ha!)?</p>
<pre><code>n = 5
# Test one.
if n == 5:
print 'Yay!'
# Test two.
if n is 5:
print 'Yay!'
</code></pre>
<p>Does this hold true for objects wher... | 299 | 2008-09-25T12:27:09Z | 134,659 | <p>Note that this is why <code>if foo is None:</code> is the preferred null comparison for python. All null objects are really pointers to the same value, which python sets aside to mean "None"</p>
<p><code>if x is True:</code> and <code>if x is False:</code> also work in a similar manner. False and True are two speci... | 21 | 2008-09-25T17:19:05Z | [
"python",
"reference",
"semantics"
] |
Is there a difference between `==` and `is` in Python? | 132,988 | <p>My <a href="http://en.wiktionary.org/wiki/Google-fu">Google-fu</a> has failed me.</p>
<p>In Python, are the following two tests for equality equivalent (ha!)?</p>
<pre><code>n = 5
# Test one.
if n == 5:
print 'Yay!'
# Test two.
if n is 5:
print 'Yay!'
</code></pre>
<p>Does this hold true for objects wher... | 299 | 2008-09-25T12:27:09Z | 1,085,652 | <p>Have a look at Stack Overflow question <em><a href="http://stackoverflow.com/questions/306313">Python's âisâ operator behaves unexpectedly with integers</a></em>.</p>
<p>What it mostly boils down to is that "<code>is</code>" checks to see if they are the same object, not just equal to each other (the numbers be... | 4 | 2009-07-06T06:20:00Z | [
"python",
"reference",
"semantics"
] |
Is there a difference between `==` and `is` in Python? | 132,988 | <p>My <a href="http://en.wiktionary.org/wiki/Google-fu">Google-fu</a> has failed me.</p>
<p>In Python, are the following two tests for equality equivalent (ha!)?</p>
<pre><code>n = 5
# Test one.
if n == 5:
print 'Yay!'
# Test two.
if n is 5:
print 'Yay!'
</code></pre>
<p>Does this hold true for objects wher... | 299 | 2008-09-25T12:27:09Z | 1,085,656 | <p>There is a simple rule of thumb to tell you when to use <code>==</code> or <code>is</code>.</p>
<ul>
<li><code>==</code> is for <em>value equality</em>. Use it when you would like to know if two objects have the same value.</li>
<li><code>is</code> is for <em>reference equality</em>. Use it when you would like to k... | 131 | 2009-07-06T06:22:52Z | [
"python",
"reference",
"semantics"
] |
Is there a difference between `==` and `is` in Python? | 132,988 | <p>My <a href="http://en.wiktionary.org/wiki/Google-fu">Google-fu</a> has failed me.</p>
<p>In Python, are the following two tests for equality equivalent (ha!)?</p>
<pre><code>n = 5
# Test one.
if n == 5:
print 'Yay!'
# Test two.
if n is 5:
print 'Yay!'
</code></pre>
<p>Does this hold true for objects wher... | 299 | 2008-09-25T12:27:09Z | 1,086,066 | <p>As John Feminella said, most of the time you will use == and != because your objective is to compare values. I'd just like to categorise what you would do the rest of the time:</p>
<p>There is one and only one instance of NoneType i.e. None is a singleton. Consequently <code>foo == None</code> and <code>foo is Non... | 2 | 2009-07-06T08:50:52Z | [
"python",
"reference",
"semantics"
] |
Is there a difference between `==` and `is` in Python? | 132,988 | <p>My <a href="http://en.wiktionary.org/wiki/Google-fu">Google-fu</a> has failed me.</p>
<p>In Python, are the following two tests for equality equivalent (ha!)?</p>
<pre><code>n = 5
# Test one.
if n == 5:
print 'Yay!'
# Test two.
if n is 5:
print 'Yay!'
</code></pre>
<p>Does this hold true for objects wher... | 299 | 2008-09-25T12:27:09Z | 31,578,945 | <p>While all these answers that rely on the implementation of objection pointer comparison vs value comparison are likely correct, there is a deeper syntactical reason for using <code>is</code> to determine if a variable value is <code>None</code> (in boolean logic often represented as <code>NULL</code>). </p>
<p>In ... | -2 | 2015-07-23T05:37:21Z | [
"python",
"reference",
"semantics"
] |
Is there a difference between `==` and `is` in Python? | 132,988 | <p>My <a href="http://en.wiktionary.org/wiki/Google-fu">Google-fu</a> has failed me.</p>
<p>In Python, are the following two tests for equality equivalent (ha!)?</p>
<pre><code>n = 5
# Test one.
if n == 5:
print 'Yay!'
# Test two.
if n is 5:
print 'Yay!'
</code></pre>
<p>Does this hold true for objects wher... | 299 | 2008-09-25T12:27:09Z | 37,155,123 | <p>Actually I wanted to add this as a comment but could not beautify it easily hence adding as an answer, please do not consider this as an answer. </p>
<p>This is what I did to understand --</p>
<blockquote>
<p>execute following one by one and understand output on every step</p>
</blockquote>
<pre><code>a = [1,2]... | 0 | 2016-05-11T06:52:25Z | [
"python",
"reference",
"semantics"
] |
Is there a difference between `==` and `is` in Python? | 132,988 | <p>My <a href="http://en.wiktionary.org/wiki/Google-fu">Google-fu</a> has failed me.</p>
<p>In Python, are the following two tests for equality equivalent (ha!)?</p>
<pre><code>n = 5
# Test one.
if n == 5:
print 'Yay!'
# Test two.
if n is 5:
print 'Yay!'
</code></pre>
<p>Does this hold true for objects wher... | 299 | 2008-09-25T12:27:09Z | 39,048,422 | <p>"==" compares values</p>
<p>"is" compares underlying objects</p>
<pre><code># this pgm is to show you the diff b/n == and is
# a==b and a is b
# == compares values
# is compares references i.e compares wether two variables refer to same object(memory)
a=10
b=10
print(a==b) # returns True as a,b have same value ... | 0 | 2016-08-19T22:19:54Z | [
"python",
"reference",
"semantics"
] |
Concurrent Access to RRD (RRDTool) | 133,774 | <p>I am using RRDTool (<a href="http://oss.oetiker.ch/rrdtool/" rel="nofollow">http://oss.oetiker.ch/rrdtool/</a>) as a graphing back-end for storing performance metrics. This is done via the RRDTool CLI from a Python script.</p>
<p>My problem is that the script is multithreaded and each thread updates the RRD at a p... | 2 | 2008-09-25T14:40:55Z | 134,023 | <p>An exclusive lock ought to be enough for this problem : </p>
<ul>
<li><a href="http://docs.python.org/lib/lock-objects.html" rel="nofollow">Python doc page</a></li>
<li><a href="http://effbot.org/zone/thread-synchronization.htm" rel="nofollow">Use example</a></li>
</ul>
<p>Define your lock object at the main leve... | 1 | 2008-09-25T15:32:22Z | [
"python",
"rrdtool"
] |
Concurrent Access to RRD (RRDTool) | 133,774 | <p>I am using RRDTool (<a href="http://oss.oetiker.ch/rrdtool/" rel="nofollow">http://oss.oetiker.ch/rrdtool/</a>) as a graphing back-end for storing performance metrics. This is done via the RRDTool CLI from a Python script.</p>
<p>My problem is that the script is multithreaded and each thread updates the RRD at a p... | 2 | 2008-09-25T14:40:55Z | 2,199,281 | <p>You could also try using rrdcached to do the updates. Then all write updates will be serialised through rrdcached. When you want to read the RRD to generate graphs you tell the daemon to flush it and the on-disk RRD will then represent the latest state.</p>
<p>All the RRD tools will do this transparently if pointed... | 2 | 2010-02-04T11:37:12Z | [
"python",
"rrdtool"
] |
Concurrent Access to RRD (RRDTool) | 133,774 | <p>I am using RRDTool (<a href="http://oss.oetiker.ch/rrdtool/" rel="nofollow">http://oss.oetiker.ch/rrdtool/</a>) as a graphing back-end for storing performance metrics. This is done via the RRDTool CLI from a Python script.</p>
<p>My problem is that the script is multithreaded and each thread updates the RRD at a p... | 2 | 2008-09-25T14:40:55Z | 3,308,401 | <p><a href="http://www.mail-archive.com/[email protected]/msg13015.html" rel="nofollow">This thread</a> in rrd-users list may be useful. The author of rrdtool states that its file locking handles concurrent reads and writes.</p>
| 1 | 2010-07-22T11:40:09Z | [
"python",
"rrdtool"
] |
Concurrent Access to RRD (RRDTool) | 133,774 | <p>I am using RRDTool (<a href="http://oss.oetiker.ch/rrdtool/" rel="nofollow">http://oss.oetiker.ch/rrdtool/</a>) as a graphing back-end for storing performance metrics. This is done via the RRDTool CLI from a Python script.</p>
<p>My problem is that the script is multithreaded and each thread updates the RRD at a p... | 2 | 2008-09-25T14:40:55Z | 26,704,889 | <p>I would suggest using <code>rrdcached</code>, which will also improve the performance of your data collector. The latest versions of <code>rrdtool</code> (1.4.x) have greatly improved the functionality and performance of <code>rrdcached</code>; you can tune the caching behaviour according to your data to optimise, ... | 0 | 2014-11-02T21:54:03Z | [
"python",
"rrdtool"
] |
Running subversion under apache and mod_python | 133,860 | <p>My Apache server runs on some non-default (not-root) account. When it tries to run a python script which in turn executes a subversion check-out command, 'svn checkout' fails with the following error message:</p>
<pre><code>svn: Can't open file '/root/.subversion/servers': Permission denied
</code></pre>
<p>At the... | 6 | 2008-09-25T15:05:26Z | 133,963 | <p>Try granting the Apache user (the user that the apache service is running under) r+w permissions on that file.</p>
| 0 | 2008-09-25T15:24:06Z | [
"python",
"svn",
"apache"
] |
Running subversion under apache and mod_python | 133,860 | <p>My Apache server runs on some non-default (not-root) account. When it tries to run a python script which in turn executes a subversion check-out command, 'svn checkout' fails with the following error message:</p>
<pre><code>svn: Can't open file '/root/.subversion/servers': Permission denied
</code></pre>
<p>At the... | 6 | 2008-09-25T15:05:26Z | 133,983 | <p>It sounds like the environment you apache process is running under is a little unusual. For whatever reason, svn seems to think the user configuration files it needs are in /root. You can avoid having svn use the root versions of the files by specifying on the command line which config directory to use, like so:</... | 5 | 2008-09-25T15:26:22Z | [
"python",
"svn",
"apache"
] |
Running subversion under apache and mod_python | 133,860 | <p>My Apache server runs on some non-default (not-root) account. When it tries to run a python script which in turn executes a subversion check-out command, 'svn checkout' fails with the following error message:</p>
<pre><code>svn: Can't open file '/root/.subversion/servers': Permission denied
</code></pre>
<p>At the... | 6 | 2008-09-25T15:05:26Z | 134,007 | <p>Doesn't Apache's error log give you a clue?</p>
<p>Maybe it has to do with SELinux. Check /var/log/audit/audit.log and adjust your SELinux configuration accordingly, if the audit.log file indicates that it's SELinux which denies Apache access.</p>
| 0 | 2008-09-25T15:30:10Z | [
"python",
"svn",
"apache"
] |
Running subversion under apache and mod_python | 133,860 | <p>My Apache server runs on some non-default (not-root) account. When it tries to run a python script which in turn executes a subversion check-out command, 'svn checkout' fails with the following error message:</p>
<pre><code>svn: Can't open file '/root/.subversion/servers': Permission denied
</code></pre>
<p>At the... | 6 | 2008-09-25T15:05:26Z | 138,587 | <p>The Permission Denied error is showing that the script is running with root credentials, because it's looking in root's home dir for files.</p>
<p>I suggest you change the hook script to something that does:</p>
<pre><code>id > /tmp/id
</code></pre>
<p>so that you can check the results of that to make sure wha... | 0 | 2008-09-26T10:06:52Z | [
"python",
"svn",
"apache"
] |
Running subversion under apache and mod_python | 133,860 | <p>My Apache server runs on some non-default (not-root) account. When it tries to run a python script which in turn executes a subversion check-out command, 'svn checkout' fails with the following error message:</p>
<pre><code>svn: Can't open file '/root/.subversion/servers': Permission denied
</code></pre>
<p>At the... | 6 | 2008-09-25T15:05:26Z | 145,501 | <p>Well, thanks to all who answered the question. Anyway, I think I solved the mistery. </p>
<p>SELinux is completely disabled on the machine, so the problem is definitely in 'svn co' not being able to found config_dir for the user account it runs under.</p>
<p>Apache / mod_python doesn't read in shell environment of... | 0 | 2008-09-28T08:53:26Z | [
"python",
"svn",
"apache"
] |
Running subversion under apache and mod_python | 133,860 | <p>My Apache server runs on some non-default (not-root) account. When it tries to run a python script which in turn executes a subversion check-out command, 'svn checkout' fails with the following error message:</p>
<pre><code>svn: Can't open file '/root/.subversion/servers': Permission denied
</code></pre>
<p>At the... | 6 | 2008-09-25T15:05:26Z | 3,105,538 | <p>What is happening is apache is being started with the environment variables of root, so it thinks that it should find its config files in /root/. This is NOT the case.
what happens is if you do sudo apache2ctl start, it pulls your $HOME variable from the sudo $HOME=/root/</p>
<p>I have just found a solution to this... | 0 | 2010-06-23T21:02:18Z | [
"python",
"svn",
"apache"
] |
Efficiently match multiple regexes in Python | 133,886 | <p>Lexical analyzers are quite easy to write when you have regexes. Today I wanted to write a simple general analyzer in Python, and came up with:</p>
<pre><code>import re
import sys
class Token(object):
""" A simple Token structure.
Contains the token type, value and position.
"""
def __init__(s... | 13 | 2008-09-25T15:10:05Z | 134,065 | <p><code>re.match</code> is anchored. You can give it a position argument:</p>
<pre><code>pos = 0
end = len(text)
while pos < end:
match = regexp.match(text, pos)
# do something with your match
pos = match.end()
</code></pre>
<p>Have a look for pygments which ships a shitload of lexers for syntax high... | 3 | 2008-09-25T15:38:49Z | [
"python",
"regex",
"lexical-analysis"
] |
Efficiently match multiple regexes in Python | 133,886 | <p>Lexical analyzers are quite easy to write when you have regexes. Today I wanted to write a simple general analyzer in Python, and came up with:</p>
<pre><code>import re
import sys
class Token(object):
""" A simple Token structure.
Contains the token type, value and position.
"""
def __init__(s... | 13 | 2008-09-25T15:10:05Z | 134,081 | <p>This isn't exactly a direct answer to your question, but you might want to look at <a href="http://www.antlr.org" rel="nofollow">ANTLR</a>. According to <a href="http://www.antlr.org/wiki/display/ANTLR3/Code+Generation+Targets" rel="nofollow">this</a> document the python code generation target should be up to date.... | 1 | 2008-09-25T15:40:47Z | [
"python",
"regex",
"lexical-analysis"
] |
Efficiently match multiple regexes in Python | 133,886 | <p>Lexical analyzers are quite easy to write when you have regexes. Today I wanted to write a simple general analyzer in Python, and came up with:</p>
<pre><code>import re
import sys
class Token(object):
""" A simple Token structure.
Contains the token type, value and position.
"""
def __init__(s... | 13 | 2008-09-25T15:10:05Z | 134,168 | <p>You can merge all your regexes into one using the "|" operator and let the regex library do the work of discerning between tokens. Some care should be taken to ensure the preference of tokens (for example to avoid matching a keyword as an identifier).</p>
| 7 | 2008-09-25T15:54:53Z | [
"python",
"regex",
"lexical-analysis"
] |
Efficiently match multiple regexes in Python | 133,886 | <p>Lexical analyzers are quite easy to write when you have regexes. Today I wanted to write a simple general analyzer in Python, and came up with:</p>
<pre><code>import re
import sys
class Token(object):
""" A simple Token structure.
Contains the token type, value and position.
"""
def __init__(s... | 13 | 2008-09-25T15:10:05Z | 135,421 | <p>It's possible that combining the token regexes will work, but you'd have to benchmark it. Something like:</p>
<pre><code>x = re.compile('(?P<NUMBER>[0-9]+)|(?P<VAR>[a-z]+)')
a = x.match('9999').groupdict() # => {'VAR': None, 'NUMBER': '9999'}
if a:
token = [a for a in a.items() if a[1] != None][0... | 3 | 2008-09-25T19:24:13Z | [
"python",
"regex",
"lexical-analysis"
] |
Efficiently match multiple regexes in Python | 133,886 | <p>Lexical analyzers are quite easy to write when you have regexes. Today I wanted to write a simple general analyzer in Python, and came up with:</p>
<pre><code>import re
import sys
class Token(object):
""" A simple Token structure.
Contains the token type, value and position.
"""
def __init__(s... | 13 | 2008-09-25T15:10:05Z | 953,837 | <p>these are not so simple, but may be worth looking at...</p>
<p>python module <strong>pyparsing</strong> (pyparsing.wikispaces.com) allows specifying grammar - then using it to parse text. Douglas, thanks for the post about <strong>ANTLR</strong> I haven't heard of it. Also there's <strong>PLY</strong> - python2 an... | 0 | 2009-06-05T01:04:16Z | [
"python",
"regex",
"lexical-analysis"
] |
Efficiently match multiple regexes in Python | 133,886 | <p>Lexical analyzers are quite easy to write when you have regexes. Today I wanted to write a simple general analyzer in Python, and came up with:</p>
<pre><code>import re
import sys
class Token(object):
""" A simple Token structure.
Contains the token type, value and position.
"""
def __init__(s... | 13 | 2008-09-25T15:10:05Z | 4,136,323 | <p>I suggest using the re.Scanner class, it's not documented in the standard library, but it's well worth using. Here's an example:</p>
<pre><code>import re
scanner = re.Scanner([
(r"-?[0-9]+\.[0-9]+([eE]-?[0-9]+)?", lambda scanner, token: float(token)),
(r"-?[0-9]+", lambda scanner, token: int(token)),
(... | 11 | 2010-11-09T16:59:03Z | [
"python",
"regex",
"lexical-analysis"
] |
Efficiently match multiple regexes in Python | 133,886 | <p>Lexical analyzers are quite easy to write when you have regexes. Today I wanted to write a simple general analyzer in Python, and came up with:</p>
<pre><code>import re
import sys
class Token(object):
""" A simple Token structure.
Contains the token type, value and position.
"""
def __init__(s... | 13 | 2008-09-25T15:10:05Z | 14,919,449 | <p>I found <a href="http://docs.python.org/3/library/re.html#writing-a-tokenizer" rel="nofollow">this</a> in python document. It's just simple and elegant.</p>
<pre><code>import collections
import re
Token = collections.namedtuple('Token', ['typ', 'value', 'line', 'column'])
def tokenize(s):
keywords = {'IF', 'T... | 6 | 2013-02-17T08:51:33Z | [
"python",
"regex",
"lexical-analysis"
] |
Run Pylons controller as separate app? | 134,387 | <p>I have a Pylons app where I would like to move some of the logic to a separate batch process. I've been running it under the main app for testing, but it is going to be doing a lot of work in the database, and I'd like it to be a separate process that will be running in the background constantly. The main pylons... | 10 | 2008-09-25T16:34:14Z | 135,290 | <p>I'm redacting my response and upvoting the other answer by Ben Bangert, as it's the correct one. I answered and have since learned the correct way (mentioned below). If you really want to, check out the history of this answer to see the wrong (but working) solution I originally proposed.</p>
| 1 | 2008-09-25T19:04:57Z | [
"python",
"pylons"
] |
Run Pylons controller as separate app? | 134,387 | <p>I have a Pylons app where I would like to move some of the logic to a separate batch process. I've been running it under the main app for testing, but it is going to be doing a lot of work in the database, and I'd like it to be a separate process that will be running in the background constantly. The main pylons... | 10 | 2008-09-25T16:34:14Z | 784,421 | <p>If you want to load parts of a Pylons app, such as the models from outside Pylons, load the Pylons app in the script first:</p>
<pre><code>from paste.deploy import appconfig
from pylons import config
from YOURPROJ.config.environment import load_environment
conf = appconfig('config:development.ini', relative_to='.... | 11 | 2009-04-24T03:42:01Z | [
"python",
"pylons"
] |
Which is more preferable to use in Python: lambda functions or nested functions ('def')? | 134,626 | <p>I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. </p>
<p>Here are some trivial examples where they functionally do the same thing if either were found within another function:</p>
<p><strong>Lambda function</strong></p>
<pre><code>>>> a = lambda x :... | 58 | 2008-09-25T17:15:03Z | 134,638 | <p>If you need to assign the <code>lambda</code> to a name, use a <code>def</code> instead. <code>def</code>s are just syntactic sugar for an assignment, so the result is the same, and they are a lot more flexible and readable.</p>
<p><code>lambda</code>s can be used for <em>use once, throw away</em> functions which w... | 65 | 2008-09-25T17:16:31Z | [
"python",
"syntax",
"function",
"lambda"
] |
Which is more preferable to use in Python: lambda functions or nested functions ('def')? | 134,626 | <p>I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. </p>
<p>Here are some trivial examples where they functionally do the same thing if either were found within another function:</p>
<p><strong>Lambda function</strong></p>
<pre><code>>>> a = lambda x :... | 58 | 2008-09-25T17:15:03Z | 134,664 | <p>I agree with nosklo's advice: if you need to give the function a name, use <code>def</code>. I reserve <code>lambda</code> functions for cases where I'm just passing a brief snippet of code to another function, e.g.:</p>
<pre><code>a = [ (1,2), (3,4), (5,6) ]
b = map( lambda x: x[0]+x[1], a )
</code></pre>
| 6 | 2008-09-25T17:19:48Z | [
"python",
"syntax",
"function",
"lambda"
] |
Which is more preferable to use in Python: lambda functions or nested functions ('def')? | 134,626 | <p>I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. </p>
<p>Here are some trivial examples where they functionally do the same thing if either were found within another function:</p>
<p><strong>Lambda function</strong></p>
<pre><code>>>> a = lambda x :... | 58 | 2008-09-25T17:15:03Z | 134,709 | <p><a href="http://www.amk.ca/python/writing/gvr-interview">In this interview, </a> Guido van Rossum says he wishes he hadn't let 'lambda' into Python:</p>
<blockquote>
<p>"<strong>Q. What feature of Python are you least pleased with?</strong><br /><br />
Sometimes I've been too quick in accepting contributions, a... | 13 | 2008-09-25T17:29:26Z | [
"python",
"syntax",
"function",
"lambda"
] |
Which is more preferable to use in Python: lambda functions or nested functions ('def')? | 134,626 | <p>I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. </p>
<p>Here are some trivial examples where they functionally do the same thing if either were found within another function:</p>
<p><strong>Lambda function</strong></p>
<pre><code>>>> a = lambda x :... | 58 | 2008-09-25T17:15:03Z | 135,353 | <p>The primary use of lambda has always been for simple callback functions, and for map, reduce, filter, which require a function as an argument. With list comprehensions becoming the norm, and the added allowed if as in:</p>
<pre><code>x = [f for f in range(1, 40) if f % 2]
</code></pre>
<p>it's hard to imagine a re... | 3 | 2008-09-25T19:13:39Z | [
"python",
"syntax",
"function",
"lambda"
] |
Which is more preferable to use in Python: lambda functions or nested functions ('def')? | 134,626 | <p>I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. </p>
<p>Here are some trivial examples where they functionally do the same thing if either were found within another function:</p>
<p><strong>Lambda function</strong></p>
<pre><code>>>> a = lambda x :... | 58 | 2008-09-25T17:15:03Z | 135,815 | <p>I agree with nosklo. By the way, even with a <em>use once, throw away</em> function, most of the time you just want to use something from the operator module.</p>
<p>E.G : </p>
<p>You have a function with this signature : myFunction(data, callback function).</p>
<p>You want to pass a function that add 2 elements.... | 0 | 2008-09-25T20:26:14Z | [
"python",
"syntax",
"function",
"lambda"
] |
Which is more preferable to use in Python: lambda functions or nested functions ('def')? | 134,626 | <p>I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. </p>
<p>Here are some trivial examples where they functionally do the same thing if either were found within another function:</p>
<p><strong>Lambda function</strong></p>
<pre><code>>>> a = lambda x :... | 58 | 2008-09-25T17:15:03Z | 138,625 | <p>Practically speaking, to me there are two differences:</p>
<p>The first is about what they do and what they return:</p>
<ul>
<li><p>def is a keyword that doesn't return anything and creates a 'name' in the local namespace.</p></li>
<li><p>lambda is a keyword that returns a function object and does not create a 'na... | 22 | 2008-09-26T10:20:43Z | [
"python",
"syntax",
"function",
"lambda"
] |
Which is more preferable to use in Python: lambda functions or nested functions ('def')? | 134,626 | <p>I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. </p>
<p>Here are some trivial examples where they functionally do the same thing if either were found within another function:</p>
<p><strong>Lambda function</strong></p>
<pre><code>>>> a = lambda x :... | 58 | 2008-09-25T17:15:03Z | 298,045 | <p>If you are just going to assign the lambda to a variable in the local scope, you may as well use def because it is more readable and can be expanded more easily in the future:</p>
<pre><code>fun = lambda a, b: a ** b # a pointless use of lambda
map(fun, someList)
</code></pre>
<p>or</p>
<pre><code>def fun(a, b): ... | 0 | 2008-11-18T07:19:32Z | [
"python",
"syntax",
"function",
"lambda"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.