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
What's the easiest way to read a FoxPro DBF file from Python?
37,535
<p>I've got a bunch of FoxPro (VFP9) DBF files on my Ubuntu system, is there a library to open these in Python? I only need to read them, and would preferably have access to the memo fields too.</p> <p><strong>Update</strong>: Thanks @cnu, I used Yusdi Santoso's <a href="http://www.physics.ox.ac.uk/users/santoso/dbf....
21
2008-09-01T06:45:40Z
578,444
<p>Check out <a href="http://groups.google.com/group/python-dbase">http://groups.google.com/group/python-dbase</a></p> <p>It currently supports dBase III and Visual Foxpro 6.0 db files... not sure if the file layout change in VFP 9 or not...</p>
5
2009-02-23T17:08:19Z
[ "python", "foxpro", "dbf", "visual-foxpro" ]
What's the easiest way to read a FoxPro DBF file from Python?
37,535
<p>I've got a bunch of FoxPro (VFP9) DBF files on my Ubuntu system, is there a library to open these in Python? I only need to read them, and would preferably have access to the memo fields too.</p> <p><strong>Update</strong>: Thanks @cnu, I used Yusdi Santoso's <a href="http://www.physics.ox.ac.uk/users/santoso/dbf....
21
2008-09-01T06:45:40Z
10,254,842
<p>I was able to read a DBF file (with associated BAK, CDX, FBT, TBK files**) using the dbf package from PyPI <a href="http://pypi.python.org/pypi/dbf">http://pypi.python.org/pypi/dbf</a> . I am new to python and know nothing about DBF files, but it worked easily to read a DBF file from my girlfriend's business (create...
8
2012-04-20T23:33:30Z
[ "python", "foxpro", "dbf", "visual-foxpro" ]
What's the easiest way to read a FoxPro DBF file from Python?
37,535
<p>I've got a bunch of FoxPro (VFP9) DBF files on my Ubuntu system, is there a library to open these in Python? I only need to read them, and would preferably have access to the memo fields too.</p> <p><strong>Update</strong>: Thanks @cnu, I used Yusdi Santoso's <a href="http://www.physics.ox.ac.uk/users/santoso/dbf....
21
2008-09-01T06:45:40Z
38,730,628
<p>It's 2016 now and I had to fiddle with the dbf package to get it to work. Here is a python3 version to just export a dbf file to a csv</p> <pre><code>import dbf d=dbf.Table('mydbf.dbf') d.open() dbf.export(d, filename='mydf_exported.csv', format='csv', header=True) </code></pre> <p>I had some unicode error at fir...
4
2016-08-02T21:18:47Z
[ "python", "foxpro", "dbf", "visual-foxpro" ]
What are Class methods in Python for?
38,238
<p>I'm teaching myself Python and my most recent lesson was that <a href="http://dirtsimple.org/2004/12/python-is-not-java.html">Python is not Java</a>, and so I've just spent a while turning all my Class methods into functions.</p> <p>I now realise that I don't need to use Class methods for what I would done with <co...
194
2008-09-01T18:16:41Z
38,254
<p>Alternative constructors are the classic example.</p>
22
2008-09-01T18:27:27Z
[ "python", "class-method" ]
What are Class methods in Python for?
38,238
<p>I'm teaching myself Python and my most recent lesson was that <a href="http://dirtsimple.org/2004/12/python-is-not-java.html">Python is not Java</a>, and so I've just spent a while turning all my Class methods into functions.</p> <p>I now realise that I don't need to use Class methods for what I would done with <co...
194
2008-09-01T18:16:41Z
38,276
<p>Class methods are for when you need to have methods that aren't specific to any particular instance, but still involve the class in some way. The most interesting thing about them is that they can be overridden by subclasses, something that's simply not possible in Java's static methods or Python's module-level func...
142
2008-09-01T18:45:56Z
[ "python", "class-method" ]
What are Class methods in Python for?
38,238
<p>I'm teaching myself Python and my most recent lesson was that <a href="http://dirtsimple.org/2004/12/python-is-not-java.html">Python is not Java</a>, and so I've just spent a while turning all my Class methods into functions.</p> <p>I now realise that I don't need to use Class methods for what I would done with <co...
194
2008-09-01T18:16:41Z
38,287
<p>Honestly? I've never found a use for staticmethod or classmethod. I've yet to see an operation that can't be done using a global function or an instance method.</p> <p>It would be different if python used private and protected members more like Java does. In Java, I need a static method to be able to access an i...
4
2008-09-01T18:54:29Z
[ "python", "class-method" ]
What are Class methods in Python for?
38,238
<p>I'm teaching myself Python and my most recent lesson was that <a href="http://dirtsimple.org/2004/12/python-is-not-java.html">Python is not Java</a>, and so I've just spent a while turning all my Class methods into functions.</p> <p>I now realise that I don't need to use Class methods for what I would done with <co...
194
2008-09-01T18:16:41Z
38,303
<p>Factory methods (alternative constructors) are indeed a classic example of class methods.</p> <p>Basically, class methods are suitable anytime you would like to have a method which naturally fits into the namespace of the class, but is not associated with a particular instance of the class.</p> <p>As an example, i...
53
2008-09-01T19:08:25Z
[ "python", "class-method" ]
What are Class methods in Python for?
38,238
<p>I'm teaching myself Python and my most recent lesson was that <a href="http://dirtsimple.org/2004/12/python-is-not-java.html">Python is not Java</a>, and so I've just spent a while turning all my Class methods into functions.</p> <p>I now realise that I don't need to use Class methods for what I would done with <co...
194
2008-09-01T18:16:41Z
2,311,096
<p>I used to work with PHP and recently I was asking myself, whats going on with this classmethod? Python manual is very technical and very short in words so it wont help with understanding that feature. I was googling and googling and I found answer -> <a href="http://code.anjanesh.net/2007/12/python-classmethods.html...
5
2010-02-22T13:27:12Z
[ "python", "class-method" ]
What are Class methods in Python for?
38,238
<p>I'm teaching myself Python and my most recent lesson was that <a href="http://dirtsimple.org/2004/12/python-is-not-java.html">Python is not Java</a>, and so I've just spent a while turning all my Class methods into functions.</p> <p>I now realise that I don't need to use Class methods for what I would done with <co...
194
2008-09-01T18:16:41Z
2,807,576
<p>I recently wanted a very light-weight logging class that would output varying amounts of output depending on the logging level that could be programmatically set. But I didn't want to instantiate the class every time I wanted to output a debugging message or error or warning. But I also wanted to encapsulate the f...
23
2010-05-11T01:48:33Z
[ "python", "class-method" ]
What are Class methods in Python for?
38,238
<p>I'm teaching myself Python and my most recent lesson was that <a href="http://dirtsimple.org/2004/12/python-is-not-java.html">Python is not Java</a>, and so I've just spent a while turning all my Class methods into functions.</p> <p>I now realise that I don't need to use Class methods for what I would done with <co...
194
2008-09-01T18:16:41Z
3,504,391
<p>Class methods provide a "semantic sugar" (don't know if this term is widely used) - or "semantic convenience".</p> <p>Example: you got a set of classes representing objects. You might want to have the class method "all" or "find" to write "User.all()" ou "User.find(firstname='Guido')". That could be done using modu...
3
2010-08-17T15:53:14Z
[ "python", "class-method" ]
What are Class methods in Python for?
38,238
<p>I'm teaching myself Python and my most recent lesson was that <a href="http://dirtsimple.org/2004/12/python-is-not-java.html">Python is not Java</a>, and so I've just spent a while turning all my Class methods into functions.</p> <p>I now realise that I don't need to use Class methods for what I would done with <co...
194
2008-09-01T18:16:41Z
3,521,920
<p>Think about it this way: normal methods are useful to hide the details of dispatch: you can type <code>myobj.foo()</code> without worrying about whether the <code>foo()</code> method is implemented by the <code>myobj</code> object's class or one of its parent classes. Class methods are exactly analogous to this, but...
37
2010-08-19T12:51:27Z
[ "python", "class-method" ]
What are Class methods in Python for?
38,238
<p>I'm teaching myself Python and my most recent lesson was that <a href="http://dirtsimple.org/2004/12/python-is-not-java.html">Python is not Java</a>, and so I've just spent a while turning all my Class methods into functions.</p> <p>I now realise that I don't need to use Class methods for what I would done with <co...
194
2008-09-01T18:16:41Z
6,001,200
<p>I think the most clear answer is <strong>AmanKow's</strong> one. It boils down to how u want to organize your code. You can write everything as module level functions which are wrapped in the namespace of the module i.e</p> <pre><code>module.py (file 1) --------- def f1() : pass def f2() : pass def f3() : pass us...
8
2011-05-14T10:24:32Z
[ "python", "class-method" ]
What are Class methods in Python for?
38,238
<p>I'm teaching myself Python and my most recent lesson was that <a href="http://dirtsimple.org/2004/12/python-is-not-java.html">Python is not Java</a>, and so I've just spent a while turning all my Class methods into functions.</p> <p>I now realise that I don't need to use Class methods for what I would done with <co...
194
2008-09-01T18:16:41Z
9,867,663
<p>When a user logs in on my website, a User() object is instantiated from the username and password.</p> <p>If I need a user object without the user being there to log in (e.g. an admin user might want to delete another users account, so i need to instantiate that user and call its delete method):</p> <p>I have clas...
6
2012-03-26T06:36:52Z
[ "python", "class-method" ]
What are Class methods in Python for?
38,238
<p>I'm teaching myself Python and my most recent lesson was that <a href="http://dirtsimple.org/2004/12/python-is-not-java.html">Python is not Java</a>, and so I've just spent a while turning all my Class methods into functions.</p> <p>I now realise that I don't need to use Class methods for what I would done with <co...
194
2008-09-01T18:16:41Z
12,312,026
<p>This is an interesting topic. My take on it is that python classmethod operates like a singleton rather than a factory (which returns a produced an instance of a class). The reason it is a singleton is that there is a common object that is produced (the dictionary) but only once for the class but shared by all insta...
2
2012-09-07T05:06:16Z
[ "python", "class-method" ]
What are Class methods in Python for?
38,238
<p>I'm teaching myself Python and my most recent lesson was that <a href="http://dirtsimple.org/2004/12/python-is-not-java.html">Python is not Java</a>, and so I've just spent a while turning all my Class methods into functions.</p> <p>I now realise that I don't need to use Class methods for what I would done with <co...
194
2008-09-01T18:16:41Z
14,042,417
<p>It allows you to write generic class methods that you can use with any compatible class.</p> <p>For example:</p> <pre><code> @classmethod def get_name(cls): print cls.name class C: name = "tester" C.get_name = get_name #call it: C.get_name() </code></pre> <p>if you dont ...
6
2012-12-26T15:11:20Z
[ "python", "class-method" ]
What are Class methods in Python for?
38,238
<p>I'm teaching myself Python and my most recent lesson was that <a href="http://dirtsimple.org/2004/12/python-is-not-java.html">Python is not Java</a>, and so I've just spent a while turning all my Class methods into functions.</p> <p>I now realise that I don't need to use Class methods for what I would done with <co...
194
2008-09-01T18:16:41Z
26,354,620
<p>I was asking myself the same question few times. And even though the guys here tried hard to explain it, IMHO the best answer (and simplest) answer I have found is the <a href="https://docs.python.org/3/library/functions.html?highlight=classmethod#classmethod" rel="nofollow">description</a> of the Class method in th...
0
2014-10-14T07:07:55Z
[ "python", "class-method" ]
What are Class methods in Python for?
38,238
<p>I'm teaching myself Python and my most recent lesson was that <a href="http://dirtsimple.org/2004/12/python-is-not-java.html">Python is not Java</a>, and so I've just spent a while turning all my Class methods into functions.</p> <p>I now realise that I don't need to use Class methods for what I would done with <co...
194
2008-09-01T18:16:41Z
39,381,275
<p>A class defines a set of instances, of course. And the methods of a class work on the individual instances. The class methods (and variables) a place to hang other information that is related to the set of instances over all.</p> <p>For example if your class defines a the set of students you might want class vari...
0
2016-09-08T01:29:19Z
[ "python", "class-method" ]
Retrieving an Oracle timestamp using Python's Win32 ODBC module
38,435
<p>Given an Oracle table created using the following:</p> <pre><code>CREATE TABLE Log(WhenAdded TIMESTAMP(6) WITH TIME ZONE); </code></pre> <p>Using the Python ODBC module from its <a href="http://www.python.org/download/windows/" rel="nofollow">Win32 extensions</a> (from the win32all package), I tried the following:...
2
2008-09-01T21:06:07Z
38,442
<p>My solution to this, that I hope can be bettered, is to use Oracle to explicitly convert the TIMESTAMP into a string:</p> <pre><code>cursor.execute("SELECT TO_CHAR(WhenAdded, 'YYYY-MM-DD HH:MI:SSAM') FROM Log") </code></pre> <p>This works, but isn't portable. I'd like to use the same Python script against a SQL Se...
1
2008-09-01T21:14:43Z
[ "python", "oracle", "ora-00932" ]
Retrieving an Oracle timestamp using Python's Win32 ODBC module
38,435
<p>Given an Oracle table created using the following:</p> <pre><code>CREATE TABLE Log(WhenAdded TIMESTAMP(6) WITH TIME ZONE); </code></pre> <p>Using the Python ODBC module from its <a href="http://www.python.org/download/windows/" rel="nofollow">Win32 extensions</a> (from the win32all package), I tried the following:...
2
2008-09-01T21:06:07Z
38,718
<p>I believe this is a bug in the Oracle ODBC driver. Basically, the Oracle ODBC driver does not support the <code>TIMESTAMP WITH (LOCAL) TIME ZONE</code> data types, only the <code>TIMESTAMP</code> data type. As you have discovered, one workaround is in fact to use the <code>TO_CHAR</code> method.</p> <p>In your exam...
2
2008-09-02T02:33:57Z
[ "python", "oracle", "ora-00932" ]
What's the best way to return multiple values from a function in Python?
38,508
<p>I have a function where I need to do something to a string. I need the function to return a boolean indicating whether or not the operation succeeded, and I also need to return the modified string. </p> <p>In C#, I would use an out parameter for the string, but there is no equivalent in Python. I'm still very ne...
49
2008-09-01T22:01:02Z
38,513
<p>Returning a tuple is the usual way to do this in Python.</p>
7
2008-09-01T22:03:17Z
[ "python", "variables", "return" ]
What's the best way to return multiple values from a function in Python?
38,508
<p>I have a function where I need to do something to a string. I need the function to return a boolean indicating whether or not the operation succeeded, and I also need to return the modified string. </p> <p>In C#, I would use an out parameter for the string, but there is no equivalent in Python. I'm still very ne...
49
2008-09-01T22:01:02Z
38,514
<p>Return a tuple.</p> <pre><code>def f(x): # do stuff return (True, modified_string) success, modified_string = f(something) </code></pre>
14
2008-09-01T22:03:41Z
[ "python", "variables", "return" ]
What's the best way to return multiple values from a function in Python?
38,508
<p>I have a function where I need to do something to a string. I need the function to return a boolean indicating whether or not the operation succeeded, and I also need to return the modified string. </p> <p>In C#, I would use an out parameter for the string, but there is no equivalent in Python. I'm still very ne...
49
2008-09-01T22:01:02Z
38,516
<pre><code>def f(in_str): out_str = in_str.upper() return True, out_str # Creates tuple automatically succeeded, b = f("a") # Automatic tuple unpacking </code></pre>
105
2008-09-01T22:04:02Z
[ "python", "variables", "return" ]
What's the best way to return multiple values from a function in Python?
38,508
<p>I have a function where I need to do something to a string. I need the function to return a boolean indicating whether or not the operation succeeded, and I also need to return the modified string. </p> <p>In C#, I would use an out parameter for the string, but there is no equivalent in Python. I'm still very ne...
49
2008-09-01T22:01:02Z
38,524
<p>Why not throw an exception if the operation wasn't successful? Personally, I tend to be of the opinion that if you need to return more than one value from a function, you should reconsider if you're doing things the right way or use an object.</p> <p>But more directly to the point, if you throw an exception, you'r...
27
2008-09-01T22:09:00Z
[ "python", "variables", "return" ]
What's the best way to return multiple values from a function in Python?
38,508
<p>I have a function where I need to do something to a string. I need the function to return a boolean indicating whether or not the operation succeeded, and I also need to return the modified string. </p> <p>In C#, I would use an out parameter for the string, but there is no equivalent in Python. I'm still very ne...
49
2008-09-01T22:01:02Z
38,760
<p>Throwing an exception for failure is one good way to proceed, and if you're returning a lot of different values, you can return a tuple. For the specific case you're citing, I often take an intermediate approach: return the modified string on success, and return None on failure. I'm enough of an unreconstructed C ...
3
2008-09-02T03:15:49Z
[ "python", "variables", "return" ]
Using Django time/date widgets in custom form
38,601
<p>How can I use the nifty JavaScript date and time widgets that the default admin uses with my custom view?</p> <p>I have looked through <a href="https://docs.djangoproject.com/en/dev/topics/forms/">the Django forms documentation</a>, and it briefly mentions django.contrib.admin.widgets, but I don't know how to use i...
147
2008-09-01T23:22:55Z
38,916
<p>The growing complexity of this answer over time, and the many hacks required, probably ought to caution you against doing this at all. It's relying on undocumented internal implementation details of the admin, is likely to break again in future versions of Django, and is no easier to implement than just finding anot...
141
2008-09-02T06:10:58Z
[ "python", "django" ]
Using Django time/date widgets in custom form
38,601
<p>How can I use the nifty JavaScript date and time widgets that the default admin uses with my custom view?</p> <p>I have looked through <a href="https://docs.djangoproject.com/en/dev/topics/forms/">the Django forms documentation</a>, and it briefly mentions django.contrib.admin.widgets, but I don't know how to use i...
147
2008-09-01T23:22:55Z
72,284
<p>As the solution is hackish, I think using your own date/time widget with some JavaScript is more feasible.</p>
57
2008-09-16T13:39:39Z
[ "python", "django" ]
Using Django time/date widgets in custom form
38,601
<p>How can I use the nifty JavaScript date and time widgets that the default admin uses with my custom view?</p> <p>I have looked through <a href="https://docs.djangoproject.com/en/dev/topics/forms/">the Django forms documentation</a>, and it briefly mentions django.contrib.admin.widgets, but I don't know how to use i...
147
2008-09-01T23:22:55Z
408,230
<p>Yep, I ended up overriding the /admin/jsi18n/ url.</p> <p>Here's what I added in my urls.py. Make sure it's above the /admin/ url</p> <pre><code> (r'^admin/jsi18n', i18n_javascript), </code></pre> <p>And here is the i18n_javascript function I created.</p> <pre><code>from django.contrib import admin def i18n_...
10
2009-01-02T22:53:21Z
[ "python", "django" ]
Using Django time/date widgets in custom form
38,601
<p>How can I use the nifty JavaScript date and time widgets that the default admin uses with my custom view?</p> <p>I have looked through <a href="https://docs.djangoproject.com/en/dev/topics/forms/">the Django forms documentation</a>, and it briefly mentions django.contrib.admin.widgets, but I don't know how to use i...
147
2008-09-01T23:22:55Z
719,583
<p>Complementing the answer by Carl Meyer, I would like to comment that you need to put that header in some valid block (inside the header) within your template.</p> <pre><code>{% block extra_head %} &lt;link rel="stylesheet" type="text/css" href="/media/admin/css/forms.css"/&gt; &lt;link rel="stylesheet" type="text/...
6
2009-04-05T20:02:00Z
[ "python", "django" ]
Using Django time/date widgets in custom form
38,601
<p>How can I use the nifty JavaScript date and time widgets that the default admin uses with my custom view?</p> <p>I have looked through <a href="https://docs.djangoproject.com/en/dev/topics/forms/">the Django forms documentation</a>, and it briefly mentions django.contrib.admin.widgets, but I don't know how to use i...
147
2008-09-01T23:22:55Z
1,392,329
<p>I find myself referencing this post a lot, and found that the <a href="http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types" rel="nofollow">documentation</a> defines a <em>slightly</em> less hacky way to override default widgets. </p> <p>(<em>No need to override the Model...
8
2009-09-08T06:42:05Z
[ "python", "django" ]
Using Django time/date widgets in custom form
38,601
<p>How can I use the nifty JavaScript date and time widgets that the default admin uses with my custom view?</p> <p>I have looked through <a href="https://docs.djangoproject.com/en/dev/topics/forms/">the Django forms documentation</a>, and it briefly mentions django.contrib.admin.widgets, but I don't know how to use i...
147
2008-09-01T23:22:55Z
1,833,247
<p>Updated solution and workaround for <strong>SplitDateTime</strong> with <strong>required=False</strong>:</p> <p><em>forms.py</em></p> <pre><code>from django import forms class SplitDateTimeJSField(forms.SplitDateTimeField): def __init__(self, *args, **kwargs): super(SplitDateTimeJSField, self).__init_...
1
2009-12-02T14:29:53Z
[ "python", "django" ]
Using Django time/date widgets in custom form
38,601
<p>How can I use the nifty JavaScript date and time widgets that the default admin uses with my custom view?</p> <p>I have looked through <a href="https://docs.djangoproject.com/en/dev/topics/forms/">the Django forms documentation</a>, and it briefly mentions django.contrib.admin.widgets, but I don't know how to use i...
147
2008-09-01T23:22:55Z
2,396,907
<p>The below will also work as a last resort if the above failed</p> <pre><code>class PaymentsForm(forms.ModelForm): class Meta: model = Payments def __init__(self, *args, **kwargs): super(PaymentsForm, self).__init__(*args, **kwargs) self.fields['date'].widget = SelectDateWidget() </code></pre> <p>...
3
2010-03-07T16:09:57Z
[ "python", "django" ]
Using Django time/date widgets in custom form
38,601
<p>How can I use the nifty JavaScript date and time widgets that the default admin uses with my custom view?</p> <p>I have looked through <a href="https://docs.djangoproject.com/en/dev/topics/forms/">the Django forms documentation</a>, and it briefly mentions django.contrib.admin.widgets, but I don't know how to use i...
147
2008-09-01T23:22:55Z
2,716,963
<p>I finally managed to get this widget working on the dev server, only to have it break on deployment. I finally decided it wasn't worth shoehorning into my site, and wrote my own widget. It's not as flexible, but it will probably work well for many: <a href="http://www.copiesofcopies.org/webl/?p=81" rel="nofollow">...
4
2010-04-26T21:17:37Z
[ "python", "django" ]
Using Django time/date widgets in custom form
38,601
<p>How can I use the nifty JavaScript date and time widgets that the default admin uses with my custom view?</p> <p>I have looked through <a href="https://docs.djangoproject.com/en/dev/topics/forms/">the Django forms documentation</a>, and it briefly mentions django.contrib.admin.widgets, but I don't know how to use i...
147
2008-09-01T23:22:55Z
2,818,128
<p>Starting in Django 1.2 RC1, if you're using the Django admin date picker widge trick, the following has to be added to your template, or you'll see the calendar icon url being referenced through "/missing-admin-media-prefix/".</p> <pre><code>{% load adminmedia %} /* At the top of the template. */ /* In the head se...
9
2010-05-12T11:03:15Z
[ "python", "django" ]
Using Django time/date widgets in custom form
38,601
<p>How can I use the nifty JavaScript date and time widgets that the default admin uses with my custom view?</p> <p>I have looked through <a href="https://docs.djangoproject.com/en/dev/topics/forms/">the Django forms documentation</a>, and it briefly mentions django.contrib.admin.widgets, but I don't know how to use i...
147
2008-09-01T23:22:55Z
3,284,874
<p>(I'm trying to comment on people suggesting to roll their own Calendar widget, but either I don't see the comment button, or I don't have enough rep.)</p> <p>What happened to <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>? I think it would be best to re-use the admin widget, but perhaps it s...
5
2010-07-19T20:38:24Z
[ "python", "django" ]
Using Django time/date widgets in custom form
38,601
<p>How can I use the nifty JavaScript date and time widgets that the default admin uses with my custom view?</p> <p>I have looked through <a href="https://docs.djangoproject.com/en/dev/topics/forms/">the Django forms documentation</a>, and it briefly mentions django.contrib.admin.widgets, but I don't know how to use i...
147
2008-09-01T23:22:55Z
9,139,017
<p>What about just assigning a class to your widget and then binding that class to the JQuery datepicker?</p> <p>Django forms.py:</p> <pre><code>class MyForm(forms.ModelForm): class Meta: model = MyModel def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) self.fields['...
2
2012-02-04T06:50:50Z
[ "python", "django" ]
Using Django time/date widgets in custom form
38,601
<p>How can I use the nifty JavaScript date and time widgets that the default admin uses with my custom view?</p> <p>I have looked through <a href="https://docs.djangoproject.com/en/dev/topics/forms/">the Django forms documentation</a>, and it briefly mentions django.contrib.admin.widgets, but I don't know how to use i...
147
2008-09-01T23:22:55Z
11,446,609
<p>My head code for 1.4 version(some new and some removed)</p> <pre><code>{% block extrahead %} &lt;link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/forms.css"/&gt; &lt;link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/base.css"/&gt; &lt;link rel="stylesheet" type="text/css" h...
8
2012-07-12T07:06:08Z
[ "python", "django" ]
Using Django time/date widgets in custom form
38,601
<p>How can I use the nifty JavaScript date and time widgets that the default admin uses with my custom view?</p> <p>I have looked through <a href="https://docs.djangoproject.com/en/dev/topics/forms/">the Django forms documentation</a>, and it briefly mentions django.contrib.admin.widgets, but I don't know how to use i...
147
2008-09-01T23:22:55Z
39,946,546
<p>In Django 10. myproject/urls.py: at the beginning of urlpatterns</p> <pre><code> from django.views.i18n import JavaScriptCatalog urlpatterns = [ url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'), . . .] </code></pre> <p>In my template.html:</p> <pre><code>{% load staticfiles %} ...
0
2016-10-09T17:39:01Z
[ "python", "django" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
38,989
<pre><code>x = {'a':1, 'b': 2} y = {'b':10, 'c': 11} z = dict(x.items() + y.items()) print z </code></pre> <p>For items with keys in both dictionaries ('b'), you can control which one ends up in the output by putting that one last.</p>
25
2008-09-02T07:49:27Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
38,990
<p>In your case, what you can do is:</p> <pre><code>z = dict(x.items() + y.items()) </code></pre> <p>This will, as you want it, put the final dict in <code>z</code>, and make the value for key <code>b</code> be properly overridden by the second (<code>y</code>) dict's value:</p> <pre><code>&gt;&gt;&gt; x = {'a':1, '...
1,292
2008-09-02T07:50:12Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
39,437
<p>An alternative:</p> <pre><code>z = x.copy() z.update(y) </code></pre>
472
2008-09-02T13:00:46Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
39,858
<p>Another, more concise, option:</p> <pre><code>z = dict(x, **y) </code></pre> <p><strong>Note</strong>: this has become a popular answer, but it is important to point out that if <code>y</code> has any non-string keys, the fact that this works at all is an abuse of a CPython implementation detail, and it does not w...
209
2008-09-02T15:52:07Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
44,512
<p>I wanted something similar, but with the ability to specify how the values on duplicate keys were merged, so I hacked this out (but did not heavily test it). Obviously this is not a single expression, but it is a single function call.</p> <pre><code>def merge(d1, d2, merge_fn=lambda x,y:y): """ Merges two ...
63
2008-09-04T19:08:25Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
49,492
<p>This probably won't be a popular answer, but you almost certainly do not want to do this. If you want a copy that's a merge, then use copy (or <a href="https://docs.python.org/2/library/copy.html">deepcopy</a>, depending on what you want) and then update. The two lines of code are much more readable - more Pythoni...
136
2008-09-08T11:16:54Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
228,366
<p>In a follow-up answer, you asked about the relative performance of these two alternatives:</p> <pre><code>z1 = dict(x.items() + y.items()) z2 = dict(x, **y) </code></pre> <p>On my machine, at least (a fairly ordinary x86_64 running Python 2.5.2), alternative <code>z2</code> is not only shorter and simpler but also...
86
2008-10-23T02:38:56Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
3,936,548
<p>The best version I could think while not using copy would be:</p> <pre><code>from itertools import chain x = {'a':1, 'b': 2} y = {'b':10, 'c': 11} dict(chain(x.iteritems(), y.iteritems())) </code></pre> <p>It's faster than <code>dict(x.items() + y.items())</code> but not as fast as <code>n = copy(a); n.update(b)</...
39
2010-10-14T18:55:15Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
7,770,473
<p>While the question has already been answered several times, this simple solution to the problem has not been listed yet.</p> <pre><code>x = {'a':1, 'b': 2} y = {'b':10, 'c': 11} z4 = {} z4.update(x) z4.update(y) </code></pre> <p>It is as fast as z0 and the evil z2 mentioned above, but easy to understand and change...
22
2011-10-14T16:12:33Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
8,247,023
<p>If you think lambdas are evil then read no further. As requested, you can write the fast and memory-efficient solution with one expression:</p> <pre><code>x = {'a':1, 'b':2} y = {'b':10, 'c':11} z = (lambda a, b: (lambda a_copy: a_copy.update(b) or a_copy)(a.copy()))(x, y) print z {'a': 1, 'c': 11, 'b': 10} print x...
15
2011-11-23T18:08:23Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
8,310,229
<h1>Recursively/deep update a dict</h1> <pre><code>def deepupdate(original, update): """ Recursively update a dict. Subdict's won't be overwritten but also updated. """ for key, value in original.iteritems(): if key not in update: update[key] = value elif isinstance(val...
32
2011-11-29T11:52:15Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
11,804,613
<p>Even though the answers were good for this <em>shallow</em> dictionary, none of the methods defined here actually do a deep dictionary merge.</p> <p>Examples follow:</p> <pre><code>a = { 'one': { 'depth_2': True }, 'two': True } b = { 'one': { 'extra': False } } print dict(a.items() + b.items()) </code></pre> <p>...
5
2012-08-03T23:36:50Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
11,825,563
<pre><code>def dict_merge(a, b): c = a.copy() c.update(b) return c new = dict_merge(old, extras) </code></pre> <p>Among such shady and dubious answers, this shining example is the one and only good way to merge dicts in Python, endorsed by dictator for life <em>Guido van Rossum</em> himself! Someone else sugge...
19
2012-08-06T09:24:44Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
12,926,103
<p><strong>Two dictionaries</strong></p> <pre><code>def union2(dict1, dict2): return dict(list(dict1.items()) + list(dict2.items())) </code></pre> <p><strong><em>n</em> dictionaries</strong></p> <pre><code>def union(*dicts): return dict(itertools.chain.from_iterable(dct.items() for dct in dicts)) </code></pr...
4
2012-10-17T02:09:45Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
16,259,217
<p>In Python 3, you can use <a href="http://docs.python.org/3/library/collections.html#collections.ChainMap"><em>collections.ChainMap</em></a> which groups multiple dicts or other mappings together to create a single, updateable view:</p> <pre><code>&gt;&gt;&gt; from collections import ChainMap &gt;&gt;&gt; x = {'a':1...
44
2013-04-28T03:15:38Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
16,769,722
<p>Using a dict comprehension, you may</p> <pre><code>x = {'a':1, 'b': 2} y = {'b':10, 'c': 11} dc = {xi:(x[xi] if xi not in list(y.keys()) else y[xi]) for xi in list(x.keys())+(list(y.keys()))} </code></pre> <p>gives</p> <pre><code>&gt;&gt;&gt; dc {'a': 1, 'c': 11, 'b': 10} </code></pre> <p>Note the ...
1
2013-05-27T09:04:20Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
17,587,183
<p>Here is some code, it seems to work ok:</p> <pre><code>def merge(d1, d2, mode=0): if not type(d2) is dict: raise Exception("d2 is not a dict") if not type(d1) is dict: if mode == 0: raise Exception("d1 is not a dict") return d2 result = dict(d1) for k, v in d2....
-2
2013-07-11T07:13:12Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
17,738,920
<p>Drawing on ideas here and elsewhere I've comprehended a function:</p> <pre><code>def merge(*dicts, **kv): return { k:v for d in list(dicts) + [kv] for k,v in d.items() } </code></pre> <p>Usage (tested in python 3):</p> <pre><code>assert (merge({1:11,'a':'aaa'},{1:99, 'b':'bbb'},foo='bar')==\ {1: 99, 'f...
5
2013-07-19T05:49:19Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
18,114,065
<p>Abuse leading to a one-expression solution for <a href="http://stackoverflow.com/a/39437/15055">Matthew's answer</a>:</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': 11} &gt;&gt;&gt; z = (lambda f=x.copy(): (f.update(y), f)[1])() &gt;&gt;&gt; z {'a': 1, 'c': 11, 'b': 10} </code></pre>...
7
2013-08-07T21:23:08Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
18,665,968
<p><code>**</code> creates an intermediary dict, which means that the total number of copies is actually higher doing the <code>dict(one, **two)</code> form, but all that happens in C so it's still generally faster than going to itertools, unless there are a huge number of copies (or, probably, if the copies are very e...
1
2013-09-06T20:18:58Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
19,279,501
<p>In python3, the <code>items</code> method <a href="http://docs.python.org/dev/whatsnew/3.0.html#views-and-iterators-instead-of-lists">no longer returns a list</a>, but rather a <em>view</em>, which acts like a set. In this case you'll need to take the set union since concatenating with <code>+</code> won't work:</p>...
10
2013-10-09T18:09:08Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
19,950,727
<pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': 11} &gt;&gt;&gt; x, z = dict(x), x.update(y) or x &gt;&gt;&gt; x {'a': 1, 'b': 2} &gt;&gt;&gt; y {'c': 11, 'b': 10} &gt;&gt;&gt; z {'a': 1, 'c': 11, 'b': 10} </code></pre>
6
2013-11-13T10:01:31Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
20,358,548
<p>The problem I have with solutions listed to date is that, in the merged dictionary, the value for key "b" is 10 but, to my way of thinking, it should be 12. In that light, I present the following:</p> <pre><code>import timeit n=100000 su = """ x = {'a':1, 'b': 2} y = {'b':10, 'c': 11} """ def timeMerge(f,su,niter...
5
2013-12-03T18:11:54Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
20,394,520
<p>I have a solution which is not specified here(Man I LOVE python) :-)</p> <pre><code>z = {} z.update(x) or z.update(y) </code></pre> <p>This will not update x as well as y. Performance? I don't think it will be terribly slow :-)</p> <p>NOTE: It is supposed to be 'or' operation and not 'and' operation. Edited to co...
1
2013-12-05T08:02:05Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
22,122,836
<p>It's so silly that <code>.update</code> returns nothing.<br> I just use a simple helper function to solve the problem:</p> <pre><code>def merge(dict1,*dicts): for dict2 in dicts: dict1.update(dict2) return dict1 </code></pre> <p>Examples:</p> <pre><code>merge(dict1,dict2) merge(dict1,dict2,dict3) ...
2
2014-03-02T01:44:39Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
26,111,877
<p>A union of the OP's two dictionaries would be something like:</p> <pre><code>{'a': 1, 'b': 2, 10, 'c': 11} </code></pre> <p>Specifically, the union of two entities(<code>x</code> and <code>y</code>) contains all the elements of <code>x</code> and/or <code>y</code>. Unfortunately, what the OP asks for is not a unio...
1
2014-09-30T02:36:19Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
26,853,961
<blockquote> <h1>How can I merge two Python dictionaries in a single expression?</h1> </blockquote> <p>Say you have two dicts and you want to merge them into a new dict without altering the original dicts:</p> <pre><code>x = {'a': 1, 'b': 2} y = {'b': 3, 'c': 4} </code></pre> <p>The desired result is to get a new ...
1,255
2014-11-10T22:11:48Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
28,753,078
<p>Python 3.5 (PEP 448) allows a nicer syntax option:</p> <pre><code>x = {'a': 1, 'b': 1} y = {'a': 2, 'c': 2} final = {**x, **y} final # {'a': 2, 'b': 1, 'c': 2} </code></pre> <p>Or even </p> <pre><code>final = {'a': 1, 'b': 1, **x, **y} </code></pre>
22
2015-02-26T21:27:52Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
29,177,685
<pre><code>a = {1: 2, 3: 4, 5: 6} b = {7:8, 1:2} combined = dict(a.items() + b.items()) print combined </code></pre>
2
2015-03-21T00:06:12Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
31,478,567
<p>This can be done with a single dict comprehension:</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': 11} &gt;&gt;&gt; { key: y[key] if key in y else x[key] for key in set(x) + set(y) } </code></pre> <p>In my view the best answer for the 'single expression' part as no extra fu...
2
2015-07-17T14:47:23Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
31,812,635
<p>Simple solution using itertools that preserves order (latter dicts have precedence)</p> <pre><code>import itertools as it merge = lambda *args: dict(it.chain.from_iterable(it.imap(dict.iteritems, args))) </code></pre> <p>And it's usage:</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c'...
7
2015-08-04T14:54:58Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
33,999,337
<pre><code>from collections import Counter dict1 = {'a':1, 'b': 2} dict2 = {'b':10, 'c': 11} result = dict(Counter(dict1) + Counter(dict2)) </code></pre> <p>This should solve your problem.</p>
1
2015-11-30T13:04:00Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
34,899,183
<p>Be pythonic. Use a <a href="https://docs.python.org/2/tutorial/datastructures.html#dictionaries" rel="nofollow">comprehension</a>:</p> <pre><code>z={i:d[i] for d in [x,y] for i in d} &gt;&gt;&gt; print z {'a': 1, 'c': 11, 'b': 10} </code></pre>
3
2016-01-20T11:46:22Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
36,263,150
<p>(For Python2.7* only; there are simpler solutions for Python3*.)</p> <p>If you're not averse to importing a standard library module, you can do</p> <pre><code>from functools import reduce def merge_dicts(*dicts): return reduce(lambda a, d: a.update(d) or a, dicts, {}) </code></pre> <p>(The <code>or a</code> ...
1
2016-03-28T13:13:27Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
37,304,637
<p>I know this does not really fit the specifics of the questions ("one liner"), but since <em>none</em> of the answers above went into this direction while lots and lots of answers addressed the performance issue, I felt I should contribute my thoughts.</p> <p>Depending on the use case it might not be necessary to cr...
1
2016-05-18T15:57:32Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
39,251,599
<p>For Python 2 :</p> <pre><code>x = {'a':1, 'b': 2} y = {'b':10, 'c': 11} z = dict(x.items()+y.items()) print(z) </code></pre> <p>For Python 3:</p> <pre><code>x = {'a':1, 'b': 2} y = {'b':10, 'c': 11} z = dict(x.items()|y.items()) print(z) </code></pre> <p>It gives output:<code>{'a': 1, 'c': 11, 'b': 10}</code></p...
0
2016-08-31T13:53:20Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
How to merge two Python dictionaries in a single expression?
38,987
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p> <pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2} &gt;&gt;&gt; y = {'b':10, 'c': ...
1,867
2008-09-02T07:44:30Z
39,736,284
<p>In Python 3.5 you can use unpack <code>**</code> in order to create new dictionary. This method has no been showed in past answers. Also, it's better to use <code>{}</code> instead of <code>dict()</code>. Because <code>{}</code> is a python literal and <code>dict()</code> involves a function call.</p> <pre><code>d...
0
2016-09-28T00:33:55Z
[ "python", "dictionary", "mapping", "expression", "idioms" ]
Search and replace a line in a file in Python
39,086
<p>I want to loop over the contents of a text file and do a search and replace on some lines and write the result back to the file. I could first load the whole file in memory and then write it back, but that probably is not the best way to do it.</p> <p>What is the best way to do this, within the following code?</p> ...
170
2008-09-02T09:19:04Z
39,089
<p>Create a new file, copy lines from the old to the new, and do the replacing before you write the lines to the new file.</p>
2
2008-09-02T09:24:20Z
[ "python", "file" ]
Search and replace a line in a file in Python
39,086
<p>I want to loop over the contents of a text file and do a search and replace on some lines and write the result back to the file. I could first load the whole file in memory and then write it back, but that probably is not the best way to do it.</p> <p>What is the best way to do this, within the following code?</p> ...
170
2008-09-02T09:19:04Z
39,110
<p>I guess something like this should do it. It basically writes the content to a new file and replaces the old file with the new file:</p> <pre><code>from tempfile import mkstemp from shutil import move from os import remove, close def replace(file_path, pattern, subst): #Create temp file fh, abs_path = mkst...
111
2008-09-02T09:42:21Z
[ "python", "file" ]
Search and replace a line in a file in Python
39,086
<p>I want to loop over the contents of a text file and do a search and replace on some lines and write the result back to the file. I could first load the whole file in memory and then write it back, but that probably is not the best way to do it.</p> <p>What is the best way to do this, within the following code?</p> ...
170
2008-09-02T09:19:04Z
39,113
<p>As lassevk suggests, write out the new file as you go, here is some example code:</p> <pre><code>fin = open("a.txt") fout = open("b.txt", "wt") for line in fin: fout.write( line.replace('foo', 'bar') ) fin.close() fout.close() </code></pre>
8
2008-09-02T09:42:57Z
[ "python", "file" ]
Search and replace a line in a file in Python
39,086
<p>I want to loop over the contents of a text file and do a search and replace on some lines and write the result back to the file. I could first load the whole file in memory and then write it back, but that probably is not the best way to do it.</p> <p>What is the best way to do this, within the following code?</p> ...
170
2008-09-02T09:19:04Z
290,494
<p>The shortest way would probably be to use the <a href="http://docs.python.org/2/library/fileinput.html?highlight=fileinput#fileinput">fileinput module</a>. For example, the following adds line numbers to a file, in-place:</p> <pre><code>import fileinput for line in fileinput.input("test.txt", inplace=True): pr...
182
2008-11-14T15:47:40Z
[ "python", "file" ]
Search and replace a line in a file in Python
39,086
<p>I want to loop over the contents of a text file and do a search and replace on some lines and write the result back to the file. I could first load the whole file in memory and then write it back, but that probably is not the best way to do it.</p> <p>What is the best way to do this, within the following code?</p> ...
170
2008-09-02T09:19:04Z
315,088
<p>Here's another example that was tested, and will match search &amp; replace patterns:</p> <pre><code>import fileinput import sys def replaceAll(file,searchExp,replaceExp): for line in fileinput.input(file, inplace=1): if searchExp in line: line = line.replace(searchExp,replaceExp) s...
52
2008-11-24T19:02:28Z
[ "python", "file" ]
Search and replace a line in a file in Python
39,086
<p>I want to loop over the contents of a text file and do a search and replace on some lines and write the result back to the file. I could first load the whole file in memory and then write it back, but that probably is not the best way to do it.</p> <p>What is the best way to do this, within the following code?</p> ...
170
2008-09-02T09:19:04Z
1,388,570
<p>This should work: (inplace editing)</p> <pre><code>import fileinput # Does a list of files, and # redirects STDOUT to the file in question for line in fileinput.input(files, inplace = 1): print line.replace("foo", "bar"), </code></pre>
30
2009-09-07T10:07:25Z
[ "python", "file" ]
Search and replace a line in a file in Python
39,086
<p>I want to loop over the contents of a text file and do a search and replace on some lines and write the result back to the file. I could first load the whole file in memory and then write it back, but that probably is not the best way to do it.</p> <p>What is the best way to do this, within the following code?</p> ...
170
2008-09-02T09:19:04Z
11,784,227
<p>if you remove the indent at the like below, it will search and replace in multiple line. See below for example.</p> <pre><code>def replace(file, pattern, subst): #Create temp file fh, abs_path = mkstemp() print fh, abs_path new_file = open(abs_path,'w') old_file = open(file) for line in old_...
0
2012-08-02T19:12:12Z
[ "python", "file" ]
Search and replace a line in a file in Python
39,086
<p>I want to loop over the contents of a text file and do a search and replace on some lines and write the result back to the file. I could first load the whole file in memory and then write it back, but that probably is not the best way to do it.</p> <p>What is the best way to do this, within the following code?</p> ...
170
2008-09-02T09:19:04Z
13,641,746
<p>Based on the answer by Thomas Watnedal. However, this does not answer the line-to-line part of the original question exactly. The function can still replace on a line-to-line basis </p> <p>This implementation replaces the file contents without using temporary files, as a consequence file permissions remain unchang...
13
2012-11-30T08:51:17Z
[ "python", "file" ]
Search and replace a line in a file in Python
39,086
<p>I want to loop over the contents of a text file and do a search and replace on some lines and write the result back to the file. I could first load the whole file in memory and then write it back, but that probably is not the best way to do it.</p> <p>What is the best way to do this, within the following code?</p> ...
170
2008-09-02T09:19:04Z
18,676,598
<p>A more pythonic way would be to use context managers like the code below:</p> <pre><code>from tempfile import mkstemp from shutil import move from os import remove def replace(source_file_path, pattern, substring): fh, target_file_path = mkstemp() with open(target_file_path, 'w') as target_file: wi...
6
2013-09-07T18:39:01Z
[ "python", "file" ]
Search and replace a line in a file in Python
39,086
<p>I want to loop over the contents of a text file and do a search and replace on some lines and write the result back to the file. I could first load the whole file in memory and then write it back, but that probably is not the best way to do it.</p> <p>What is the best way to do this, within the following code?</p> ...
170
2008-09-02T09:19:04Z
21,857,132
<p>If you're wanting a generic function that replaces <em>any</em> text with some other text, this is likely the best way to go, particularly if you're a fan of regex's:</p> <pre><code>import re def replace( filePath, text, subs, flags=0 ): with open( filePath, "r+" ) as file: fileContents = file.read() ...
7
2014-02-18T14:43:02Z
[ "python", "file" ]
Search and replace a line in a file in Python
39,086
<p>I want to loop over the contents of a text file and do a search and replace on some lines and write the result back to the file. I could first load the whole file in memory and then write it back, but that probably is not the best way to do it.</p> <p>What is the best way to do this, within the following code?</p> ...
170
2008-09-02T09:19:04Z
23,123,426
<p>Using hamishmcn's answer as a template I was able to search for a line in a file that match my regex and replacing it with empty string.</p> <pre><code>import re fin = open("in.txt", 'r') # in file fout = open("out.txt", 'w') # out file for line in fin: p = re.compile('[-][0-9]*[.][0-9]*[,]|[-][0-9]*[,]') # p...
0
2014-04-17T02:13:32Z
[ "python", "file" ]
Search and replace a line in a file in Python
39,086
<p>I want to loop over the contents of a text file and do a search and replace on some lines and write the result back to the file. I could first load the whole file in memory and then write it back, but that probably is not the best way to do it.</p> <p>What is the best way to do this, within the following code?</p> ...
170
2008-09-02T09:19:04Z
23,426,834
<p>Expanding on @Kiran's answer, which I agree is more succinct and Pythonic, this adds codecs to support the reading and writing of UTF-8:</p> <pre><code>import codecs from tempfile import mkstemp from shutil import move from os import remove def replace(source_file_path, pattern, substring): fh, target_file_...
2
2014-05-02T11:15:57Z
[ "python", "file" ]
Finding a file in a Python module distribution
39,104
<p>I've written a Python package that includes a bsddb database of pre-computed values for one of the more time-consuming computations. For simplicity, my setup script installs the database file in the same directory as the code which accesses the database (on Unix, something like /usr/lib/python2.5/site-packages/mypa...
12
2008-09-02T09:40:26Z
39,295
<p>That's probably the way to do it, without resorting to something more advanced like using setuptools to install the files where they belong.</p> <p>Notice there's a problem with that approach, because on OSes with real a security framework (UNIXes, etc.) the user running your script might not have the rights to acc...
4
2008-09-02T11:43:45Z
[ "python", "distutils" ]
Finding a file in a Python module distribution
39,104
<p>I've written a Python package that includes a bsddb database of pre-computed values for one of the more time-consuming computations. For simplicity, my setup script installs the database file in the same directory as the code which accesses the database (on Unix, something like /usr/lib/python2.5/site-packages/mypa...
12
2008-09-02T09:40:26Z
39,659
<p>Try using pkg_resources, which is part of setuptools (and available on all of the pythons I have access to right now):</p> <pre><code>&gt;&gt;&gt; import pkg_resources &gt;&gt;&gt; pkg_resources.resource_ filename(__name__, "foo.config") 'foo.config' &gt;&gt;&gt; pkg_resources.resource_filename('tempfile', "foo.con...
14
2008-09-02T14:26:07Z
[ "python", "distutils" ]
Finding a file in a Python module distribution
39,104
<p>I've written a Python package that includes a bsddb database of pre-computed values for one of the more time-consuming computations. For simplicity, my setup script installs the database file in the same directory as the code which accesses the database (on Unix, something like /usr/lib/python2.5/site-packages/mypa...
12
2008-09-02T09:40:26Z
9,918,496
<p>Use <code>pkgutil.get_data</code>. It’s the cousin of <code>pkg_resources.resource_stream</code>, but in the standard library, and should work with flat filesystem installs as well as zipped packages and other importers.</p>
9
2012-03-29T02:52:42Z
[ "python", "distutils" ]
What is the best way to do Bit Field manipulation in Python?
39,663
<p>I'm reading some MPEG Transport Stream protocol over UDP and it has some funky bitfields in it (length 13 for example). I'm using the "struct" library to do the broad unpacking, but is there a simple way to say "Grab the next 13 bits" rather than have to hand-tweak the bit manipulation? I'd like something like the w...
25
2008-09-02T14:28:40Z
39,760
<p>It's an often-asked question. There's an <a href="http://code.activestate.com/recipes/113799/">ASPN Cookbook</a> entry on it that has served me in the past.</p> <p>And there is an <a href="http://wiki.python.org/moin/BitManipulation">extensive page of requirements one person would like to see from a module doing t...
7
2008-09-02T15:13:44Z
[ "python", "udp", "bits", "bit-fields" ]
What is the best way to do Bit Field manipulation in Python?
39,663
<p>I'm reading some MPEG Transport Stream protocol over UDP and it has some funky bitfields in it (length 13 for example). I'm using the "struct" library to do the broad unpacking, but is there a simple way to say "Grab the next 13 bits" rather than have to hand-tweak the bit manipulation? I'd like something like the w...
25
2008-09-02T14:28:40Z
1,086,668
<p>The <a href="http://python-bitstring.googlecode.com">bitstring</a> module is designed to address just this problem. It will let you read, modify and construct data using bits as the basic building blocks. The latest versions are for Python 2.6 or later (including Python 3) but version 1.0 supported Python 2.4 and 2....
25
2009-07-06T12:20:13Z
[ "python", "udp", "bits", "bit-fields" ]
Using C in a shared multi-platform POSIX environment
39,847
<p>I write tools that are used in a shared workspace. Since there are multiple OS's working in this space, we generally use Python and standardize the version that is installed across machines. However, if I wanted to write some things in C, I was wondering if maybe I could have the application wrapped in a Python scri...
2
2008-09-02T15:46:39Z
39,865
<p>You know, you should look at static linking.</p> <p>These days, we all have HUGE hard drives, and a few extra megabytes (for carrying around libc and what not) is really not that big a deal anymore. </p> <p>You could also try running your applications in chroot() jails and distributing those.</p>
0
2008-09-02T15:58:10Z
[ "python", "c", "cross-platform", "posix", "scripting" ]
Using C in a shared multi-platform POSIX environment
39,847
<p>I write tools that are used in a shared workspace. Since there are multiple OS's working in this space, we generally use Python and standardize the version that is installed across machines. However, if I wanted to write some things in C, I was wondering if maybe I could have the application wrapped in a Python scri...
2
2008-09-02T15:46:39Z
39,871
<p>Depending on your mix os OSes, you might be better off creating packages for each class of system.</p> <p>Alternatively, if they all share the same ABI and hardware architecture, you could also compile static binaries.</p>
0
2008-09-02T15:59:52Z
[ "python", "c", "cross-platform", "posix", "scripting" ]
Using C in a shared multi-platform POSIX environment
39,847
<p>I write tools that are used in a shared workspace. Since there are multiple OS's working in this space, we generally use Python and standardize the version that is installed across machines. However, if I wanted to write some things in C, I was wondering if maybe I could have the application wrapped in a Python scri...
2
2008-09-02T15:46:39Z
39,878
<p>Also, you could use autoconf and distribute your application in source form only. :)</p>
1
2008-09-02T16:03:04Z
[ "python", "c", "cross-platform", "posix", "scripting" ]
Using C in a shared multi-platform POSIX environment
39,847
<p>I write tools that are used in a shared workspace. Since there are multiple OS's working in this space, we generally use Python and standardize the version that is installed across machines. However, if I wanted to write some things in C, I was wondering if maybe I could have the application wrapped in a Python scri...
2
2008-09-02T15:46:39Z
40,367
<p>Launching a Python interpreter instance just to select the right binary to run would be much heavier than you need. I'd distribute a shell .rc file which provides aliases.</p> <p>In /shared/bin, you put the various binaries: /shared/bin/toolname-mac, /shared/bin/toolname-debian-x86, /shared/bin/toolname-netbsd-dre...
2
2008-09-02T19:29:07Z
[ "python", "c", "cross-platform", "posix", "scripting" ]
PGP signatures from Python?
39,929
<p>What is the easiest way to create and verify PGP/GPG signatures from within a Python application? </p> <p>I can call pgp or gpg using subprocess and parse the output, but I was looking for a way that didn't require an external program to be installed (my application is cross-platform mac/windows/unix).</p>
5
2008-09-02T16:19:11Z
40,069
<p>I think <a href="http://www.gnupg.org/related_software/gpgme/" rel="nofollow">GPGME</a> and the <a href="http://pyme.sourceforge.net/" rel="nofollow">PyMe Python wrapper</a> should do what you need.</p>
8
2008-09-02T17:15:23Z
[ "python", "security", "cross-platform", "gnupg" ]
PGP signatures from Python?
39,929
<p>What is the easiest way to create and verify PGP/GPG signatures from within a Python application? </p> <p>I can call pgp or gpg using subprocess and parse the output, but I was looking for a way that didn't require an external program to be installed (my application is cross-platform mac/windows/unix).</p>
5
2008-09-02T16:19:11Z
4,849,894
<p>In addition to PyMe, consider <a href="http://code.google.com/p/python-gnupg/" rel="nofollow">python-gnupg</a> and the <a href="http://py-gnupg.sourceforge.net/" rel="nofollow">gnupginterface</a>.</p>
2
2011-01-31T10:37:45Z
[ "python", "security", "cross-platform", "gnupg" ]
javascript locals()?
39,960
<p>In python one can get a dictionary of all local and global variables in the current scope with the built-in functions locals() and globals(). Is there some equivalent way of doing this in javascript? For instance, I would like to do something like the following:</p> <pre><code>var foo = function(){ alert('foo'); ...
17
2008-09-02T16:29:30Z
39,982
<p>Well, I don't think that there is something like that in js. You can always use eval instead of locals(). Like this: </p> <pre><code>eval(s+"()"); </code></pre> <p>You just have to know that actually function <em>foo</em> exists.</p> <p><strong>Edit:</strong></p> <p>Don't use eval:) Use:</p> <pre><code>var func...
4
2008-09-02T16:36:28Z
[ "javascript", "python" ]
javascript locals()?
39,960
<p>In python one can get a dictionary of all local and global variables in the current scope with the built-in functions locals() and globals(). Is there some equivalent way of doing this in javascript? For instance, I would like to do something like the following:</p> <pre><code>var foo = function(){ alert('foo'); ...
17
2008-09-02T16:29:30Z
39,992
<p>AFAIK, no. If you just want to check the existence of a given variable, you can do it by testing for it, something like this:</p> <pre><code>if (foo) foo(); </code></pre>
0
2008-09-02T16:41:56Z
[ "javascript", "python" ]
javascript locals()?
39,960
<p>In python one can get a dictionary of all local and global variables in the current scope with the built-in functions locals() and globals(). Is there some equivalent way of doing this in javascript? For instance, I would like to do something like the following:</p> <pre><code>var foo = function(){ alert('foo'); ...
17
2008-09-02T16:29:30Z
40,012
<p>I seem to remember Brendan Eich commented on this in a recent podcast; if i recall correctly, it's not being considered, as it adds unreasonable restrictions to optimization. He compared it to the <code>arguments</code> local in that, while useful for varargs, its very existence removes the ability to guess at what ...
3
2008-09-02T16:50:34Z
[ "javascript", "python" ]