log_score
int64
-1
11
ViewCount
int64
8
6.81M
A_Id
int64
1.99k
72.5M
Available Count
int64
1
18
Question
stringlengths
15
29k
AnswerCount
int64
1
51
Q_Score
int64
0
6.79k
is_accepted
bool
2 classes
Tags
stringlengths
6
94
CreationDate
stringlengths
23
23
Q_Id
int64
1.98k
70M
Title
stringlengths
11
150
Answer
stringlengths
6
11.6k
Score
float64
-1
1.2
Users Score
int64
-36
1.15k
2
11,654
51,512
2
Besides the dynamic nature of Python (and the syntax), what are some of the major features of the Python language that Java doesn't have, and vice versa?
5
29
false
java,python
2008-09-08T14:36:00.000
49,824
Java -> Python?
Apart from what Eli Courtwright said: I find iterators in Python more concise. You can use for i in something, and it works with pretty much everything. Yeah, Java has gotten better since 1.5, but for example you can iterate through a string in python with this same construct. Introspection: In python you can get at runtime information about an object or a module about its symbols, methods, or even its docstrings. You can also instantiate them dynamically. Java has some of this, but usually in Java it takes half a page of code to get an instance of a class, whereas in Python it is about 3 lines. And as far as I know the docstrings thing is not available in Java
0.07983
2
-1
10,861
108,603
2
I have 2 time values which have the type datetime.time. I want to find their difference. The obvious thing to do is t1 - t2, but this doesn't work. It works for objects of type datetime.datetime but not for datetime.time. So what is the best way to do this?
7
13
false
python,datetime,time
2008-09-09T00:28:00.000
51,010
What is the simplest way to find the difference between 2 times in python?
Retrieve the times in milliseconds and then do the subtraction.
-0.028564
-1
-1
10,861
263,451
2
I have 2 time values which have the type datetime.time. I want to find their difference. The obvious thing to do is t1 - t2, but this doesn't work. It works for objects of type datetime.datetime but not for datetime.time. So what is the best way to do this?
7
13
false
python,datetime,time
2008-09-09T00:28:00.000
51,010
What is the simplest way to find the difference between 2 times in python?
Environment.TickCount seems to work well if you need something quick. int start = Environment.TickCount ...DoSomething() int elapsedtime = Environment.TickCount - start Jon
-0.085505
-3
2
1,296
199,275
3
Having tries to target two of these environments at the same time I can safely say the if you have to use a database etc. you end up having to write unique code for that environment. Have you got a great way to handle this situation?
6
16
false
python,ironpython,jython,cpython
2008-09-10T07:04:00.000
53,543
What are some strategies to write python code that works in CPython, Jython and IronPython
I write code for CPython and IronPython but tip should work for Jython as well. Basically, I write all the platform specific code in separate modules/packages and then import the appropriate one based on platform I'm running on. (see cdleary's comment above) This is especially important when it comes to the differences between the SQLite implementations and if you are implementing any GUI code.
0.066568
2
0
1,296
637,549
3
Having tries to target two of these environments at the same time I can safely say the if you have to use a database etc. you end up having to write unique code for that environment. Have you got a great way to handle this situation?
6
16
false
python,ironpython,jython,cpython
2008-09-10T07:04:00.000
53,543
What are some strategies to write python code that works in CPython, Jython and IronPython
There are two major issues at play here... Firstly, to my knowledge, only CPython has RAII - you have to close your own resources in Jython, Ironpython, etc. And Secondly, as has been mentioned, is thread safety.
0
0
1
1,296
342,835
3
Having tries to target two of these environments at the same time I can safely say the if you have to use a database etc. you end up having to write unique code for that environment. Have you got a great way to handle this situation?
6
16
false
python,ironpython,jython,cpython
2008-09-10T07:04:00.000
53,543
What are some strategies to write python code that works in CPython, Jython and IronPython
The #1 thing IMO: Focus on thread safety. CPython's GIL makes writing threadsafe code easy because only one thread can access the interpreter at a time. IronPython and Jython are a little less hand-holding though.
0.033321
1
3
257,866
16,295,402
2
What is the difference between old style and new style classes in Python? When should I use one or the other?
8
1,091
false
python,class,oop,types,new-style-class
2008-09-10T18:01:00.000
54,867
What is the difference between old style and new style classes in Python?
New style classes may use super(Foo, self) where Foo is a class and self is the instance. super(type[, object-or-type]) Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used by getattr() except that the type itself is skipped. And in Python 3.x you can simply use super() inside a class without any parameters.
1
8
5
257,866
3,228,045
2
What is the difference between old style and new style classes in Python? When should I use one or the other?
8
1,091
false
python,class,oop,types,new-style-class
2008-09-10T18:01:00.000
54,867
What is the difference between old style and new style classes in Python?
Old style classes are still marginally faster for attribute lookup. This is not usually important, but it may be useful in performance-sensitive Python 2.x code: In [3]: class A: ...: def __init__(self): ...: self.a = 'hi there' ...: In [4]: class B(object): ...: def __init__(self): ...: self.a = 'hi there' ...: In [6]: aobj = A() In [7]: bobj = B() In [8]: %timeit aobj.a 10000000 loops, best of 3: 78.7 ns per loop In [10]: %timeit bobj.a 10000000 loops, best of 3: 86.9 ns per loop
1
41
4
1,983
56,008
1
I use Emacs primarily for coding Python but sometimes I use IDLE. Is there a way to change the key bindings easily in IDLE to match Emacs?
3
6
true
python,emacs,ide,keyboard
2008-09-10T21:32:00.000
55,365
How can I get Emacs' key bindings in Python's IDLE?
IDLE provides Emacs keybindings without having to install other software. Open up the menu item Options -> Configure IDLE... Go to Keys tab In the drop down menu on the right side of the dialog change the select to "IDLE Classic Unix" It's not the true emacs key bindings but you get the basics like movement, saving/opening, ...
1.2
6
1
712,544
16,048,319
12
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?
19
718
false
python,coding-style
2008-09-11T08:18:00.000
56,011
Single quotes vs. double quotes in Python
I aim to minimize both pixels and surprise. I typically prefer ' in order to minimize pixels, but " instead if the string has an apostrophe, again to minimize pixels. For a docstring, however, I prefer """ over ''' because the latter is non-standard, uncommon, and therefore surprising. If now I have a bunch of strings where I used " per the above logic, but also one that can get away with a ', I may still use " in it to preserve consistency, only to minimize surprise. Perhaps it helps to think of the pixel minimization philosophy in the following way. Would you rather that English characters looked like A B C or AA BB CC? The latter choice wastes 50% of the non-empty pixels.
0
0
2
712,544
629,106
12
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?
19
718
false
python,coding-style
2008-09-11T08:18:00.000
56,011
Single quotes vs. double quotes in Python
I chose to use double quotes because they are easier to see.
0.010526
1
0
712,544
3,179,568
12
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?
19
718
false
python,coding-style
2008-09-11T08:18:00.000
56,011
Single quotes vs. double quotes in Python
In Perl you want to use single quotes when you have a string which doesn't need to interpolate variables or escaped characters like \n, \t, \r, etc. PHP makes the same distinction as Perl: content in single quotes will not be interpreted (not even \n will be converted), as opposed to double quotes which can contain variables to have their value printed out. Python does not, I'm afraid. Technically seen, there is no $ token (or the like) to separate a name/text from a variable in Python. Both features make Python more readable, less confusing, after all. Single and double quotes can be used interchangeably in Python.
0.02105
2
5
712,544
104,842
12
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?
19
718
false
python,coding-style
2008-09-11T08:18:00.000
56,011
Single quotes vs. double quotes in Python
It's probably a stylistic preference more than anything. I just checked PEP 8 and didn't see any mention of single versus double quotes. I prefer single quotes because its only one keystroke instead of two. That is, I don't have to mash the shift key to make single quote.
0.02105
2
-1
712,544
145,149
12
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?
19
718
false
python,coding-style
2008-09-11T08:18:00.000
56,011
Single quotes vs. double quotes in Python
Personally I stick with one or the other. It doesn't matter. And providing your own meaning to either quote is just to confuse other people when you collaborate.
0.031568
3
5
712,544
56,029
12
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?
19
718
false
python,coding-style
2008-09-11T08:18:00.000
56,011
Single quotes vs. double quotes in Python
None as far as I know. Although if you look at some code, " " is commonly used for strings of text (I guess ' is more common inside text than "), and ' ' appears in hashkeys and things like that.
0
0
0
712,544
56,041
12
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?
19
718
false
python,coding-style
2008-09-11T08:18:00.000
56,011
Single quotes vs. double quotes in Python
If the string you have contains one, then you should use the other. For example, "You're able to do this", or 'He said "Hi!"'. Other than that, you should simply be as consistent as you can (within a module, within a package, within a project, within an organisation). If your code is going to be read by people who work with C/C++ (or if you switch between those languages and Python), then using '' for single-character strings, and "" for longer strings might help ease the transition. (Likewise for following other languages where they are not interchangeable). The Python code I've seen in the wild tends to favour " over ', but only slightly. The one exception is that """these""" are much more common than '''these''', from what I have seen.
1
26
2
712,544
3,179,675
12
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?
19
718
false
python,coding-style
2008-09-11T08:18:00.000
56,011
Single quotes vs. double quotes in Python
I use double quotes because I have been doing so for years in most languages (C++, Java, VB…) except Bash, because I also use double quotes in normal text and because I'm using a (modified) non-English keyboard where both characters require the shift key.
-0.010526
-1
1
712,544
56,210
12
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?
19
718
false
python,coding-style
2008-09-11T08:18:00.000
56,011
Single quotes vs. double quotes in Python
I'm with Will: Double quotes for text Single quotes for anything that behaves like an identifier Double quoted raw string literals for regexps Tripled double quotes for docstrings I'll stick with that even if it means a lot of escaping. I get the most value out of single quoted identifiers standing out because of the quotes. The rest of the practices are there just to give those single quoted identifiers some standing room.
1
44
0
712,544
56,025
12
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?
19
718
false
python,coding-style
2008-09-11T08:18:00.000
56,011
Single quotes vs. double quotes in Python
Your team's taste or your project's coding guidelines. If you are in a multilanguage environment, you might wish to encourage the use of the same type of quotes for strings that the other language uses, for instance. Else, I personally like best the look of '
0
0
2
712,544
56,047
12
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?
19
718
false
python,coding-style
2008-09-11T08:18:00.000
56,011
Single quotes vs. double quotes in Python
I use double quotes in general, but not for any specific reason - Probably just out of habit from Java. I guess you're also more likely to want apostrophes in an inline literal string than you are to want double quotes.
0.031568
3
2
712,544
1,167,795
12
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?
19
718
false
python,coding-style
2008-09-11T08:18:00.000
56,011
Single quotes vs. double quotes in Python
I just use whatever strikes my fancy at the time; it's convenient to be able to switch between the two at a whim! Of course, when quoting quote characetrs, switching between the two might not be so whimsical after all...
0.010526
1
3
1,565
60,431
1
If all of my __init__.py files are empty, do I have to store them into version control, or is there a way to make distutils create empty __init__.py files during installation?
2
0
false
python,version-control,distutils
2008-09-13T05:51:00.000
60,352
Can distutils create empty __init__.py files?
In Python, __init__.py files actually have a meaning! They mean that the folder they are in is a Python module. As such, they have a real role in your code and should most probably be stored in Version Control. You could well imagine a folder in your source tree that is NOT a Python module, for example a folder containing only resources (e.g. images) and no code. That folder would not need to have a __init__.py file in it. Now how do you make the difference between folders where distutils should create those files and folders where it should not ?
1
7
0
47,709
103,211
4
Is it possible to retrieve items from a Python dictionary in the order that they were inserted?
11
65
false
python,dictionary
2008-09-13T20:38:00.000
60,848
How do you retrieve items from a dictionary in the order that they're inserted?
It's not possible unless you store the keys in a separate list for referencing later.
0
0
3
47,709
65,991
4
Is it possible to retrieve items from a Python dictionary in the order that they were inserted?
11
65
false
python,dictionary
2008-09-13T20:38:00.000
60,848
How do you retrieve items from a dictionary in the order that they're inserted?
Or, just make the key a tuple with time.now() as the first field in the tuple. Then you can retrieve the keys with dictname.keys(), sort, and voila! Gerry
0.090659
5
3
47,709
60,852
4
Is it possible to retrieve items from a Python dictionary in the order that they were inserted?
11
65
false
python,dictionary
2008-09-13T20:38:00.000
60,848
How do you retrieve items from a dictionary in the order that they're inserted?
You can't do this with the base dict class -- it's ordered by hash. You could build your own dictionary that is really a list of key,value pairs or somesuch, which would be ordered.
0.090659
5
-1
47,709
64,266
4
Is it possible to retrieve items from a Python dictionary in the order that they were inserted?
11
65
false
python,dictionary
2008-09-13T20:38:00.000
60,848
How do you retrieve items from a dictionary in the order that they're inserted?
if you don't need the dict functionality, and only need to return tuples in the order you've inserted them, wouldn't a queue work better?
-0.01818
-1
1
112,049
61,168
4
If you're writing a library, or an app, where do the unit test files go? It's nice to separate the test files from the main app code, but it's awkward to put them into a "tests" subdirectory inside of the app root directory, because it makes it harder to import the modules that you'll be testing. Is there a best practice here?
18
545
false
python,unit-testing,code-organization
2008-09-14T05:41:00.000
61,151
Where do the Python unit tests go?
I don't believe there is an established "best practice". I put my tests in another directory outside of the app code. I then add the main app directory to sys.path (allowing you to import the modules from anywhere) in my test runner script (which does some other stuff as well) before running all the tests. This way I never have to remove the tests directory from the main code when I release it, saving me time and effort, if an ever so tiny amount.
1
13
0
112,049
61,518
4
If you're writing a library, or an app, where do the unit test files go? It's nice to separate the test files from the main app code, but it's awkward to put them into a "tests" subdirectory inside of the app root directory, because it makes it harder to import the modules that you'll be testing. Is there a best practice here?
18
545
false
python,unit-testing,code-organization
2008-09-14T05:41:00.000
61,151
Where do the Python unit tests go?
I've recently started to program in Python, so I've not really had chance to find out best practice yet. But, I've written a module that goes and finds all the tests and runs them. So, I have: app/ appfile.py test/ appfileTest.py I'll have to see how it goes as I progress to larger projects.
-0.022219
-2
-1
112,049
61,820
4
If you're writing a library, or an app, where do the unit test files go? It's nice to separate the test files from the main app code, but it's awkward to put them into a "tests" subdirectory inside of the app root directory, because it makes it harder to import the modules that you'll be testing. Is there a best practice here?
18
545
false
python,unit-testing,code-organization
2008-09-14T05:41:00.000
61,151
Where do the Python unit tests go?
In C#, I've generally separated the tests into a separate assembly. In Python -- so far -- I've tended to either write doctests, where the test is in the docstring of a function, or put them in the if __name__ == "__main__" block at the bottom of the module.
0.011111
1
4
112,049
63,645
4
If you're writing a library, or an app, where do the unit test files go? It's nice to separate the test files from the main app code, but it's awkward to put them into a "tests" subdirectory inside of the app root directory, because it makes it harder to import the modules that you'll be testing. Is there a best practice here?
18
545
false
python,unit-testing,code-organization
2008-09-14T05:41:00.000
61,151
Where do the Python unit tests go?
When writing a package called "foo", I will put unit tests into a separate package "foo_test". Modules and subpackages will then have the same name as the SUT package module. E.g. tests for a module foo.x.y are found in foo_test.x.y. The __init__.py files of each testing package then contain an AllTests suite that includes all test suites of the package. setuptools provides a convenient way to specify the main testing package, so that after "python setup.py develop" you can just use "python setup.py test" or "python setup.py test -s foo_test.x.SomeTestSuite" to the just a specific suite.
0
0
1
238
61,570
1
What is the best available method for developing a spell check engine (for example, with aspell_python), that works with apache mod_python? apache 2.0.59+RHEL4+mod_python+aspell_python seems to crash. Is there any alternative to using aspell_python?
1
0
false
spell-checking,mod-python,aspell
2008-09-14T18:58:00.000
61,556
Spell Checking Service with python using mod_python
Looks like RHEL4 is the culprit. Works well on Fedore 7 (the version of apache is newer and there is no crash)
0.197375
1
0
109,190
62,592
4
What is the easiest way to compare strings in Python, ignoring case? Of course one can do (str1.lower() <= str2.lower()), etc., but this created two additional temporary strings (with the obvious alloc/g-c overheads). I guess I'm looking for an equivalent to C's stricmp(). [Some more context requested, so I'll demonstrate with a trivial example:] Suppose you want to sort a looong list of strings. You simply do theList.sort(). This is O(n * log(n)) string comparisons and no memory management (since all strings and list elements are some sort of smart pointers). You are happy. Now, you want to do the same, but ignore the case (let's simplify and say all strings are ascii, so locale issues can be ignored). You can do theList.sort(key=lambda s: s.lower()), but then you cause two new allocations per comparison, plus burden the garbage-collector with the duplicated (lowered) strings. Each such memory-management noise is orders-of-magnitude slower than simple string comparison. Now, with an in-place stricmp()-like function, you do: theList.sort(cmp=stricmp) and it is as fast and as memory-friendly as theList.sort(). You are happy again. The problem is any Python-based case-insensitive comparison involves implicit string duplications, so I was expecting to find a C-based comparisons (maybe in module string). Could not find anything like that, hence the question here. (Hope this clarifies the question).
16
52
false
python,string,case-insensitive
2008-09-15T12:57:00.000
62,567
Ignore case in Python strings
I'm pretty sure you either have to use .lower() or use a regular expression. I'm not aware of a built-in case-insensitive string comparison function.
0
0
-1
109,190
62,652
4
What is the easiest way to compare strings in Python, ignoring case? Of course one can do (str1.lower() <= str2.lower()), etc., but this created two additional temporary strings (with the obvious alloc/g-c overheads). I guess I'm looking for an equivalent to C's stricmp(). [Some more context requested, so I'll demonstrate with a trivial example:] Suppose you want to sort a looong list of strings. You simply do theList.sort(). This is O(n * log(n)) string comparisons and no memory management (since all strings and list elements are some sort of smart pointers). You are happy. Now, you want to do the same, but ignore the case (let's simplify and say all strings are ascii, so locale issues can be ignored). You can do theList.sort(key=lambda s: s.lower()), but then you cause two new allocations per comparison, plus burden the garbage-collector with the duplicated (lowered) strings. Each such memory-management noise is orders-of-magnitude slower than simple string comparison. Now, with an in-place stricmp()-like function, you do: theList.sort(cmp=stricmp) and it is as fast and as memory-friendly as theList.sort(). You are happy again. The problem is any Python-based case-insensitive comparison involves implicit string duplications, so I was expecting to find a C-based comparisons (maybe in module string). Could not find anything like that, hence the question here. (Hope this clarifies the question).
16
52
false
python,string,case-insensitive
2008-09-15T12:57:00.000
62,567
Ignore case in Python strings
You could subclass str and create your own case-insenstive string class but IMHO that would be extremely unwise and create far more trouble than it's worth.
-0.012499
-1
0
109,190
62,983
4
What is the easiest way to compare strings in Python, ignoring case? Of course one can do (str1.lower() <= str2.lower()), etc., but this created two additional temporary strings (with the obvious alloc/g-c overheads). I guess I'm looking for an equivalent to C's stricmp(). [Some more context requested, so I'll demonstrate with a trivial example:] Suppose you want to sort a looong list of strings. You simply do theList.sort(). This is O(n * log(n)) string comparisons and no memory management (since all strings and list elements are some sort of smart pointers). You are happy. Now, you want to do the same, but ignore the case (let's simplify and say all strings are ascii, so locale issues can be ignored). You can do theList.sort(key=lambda s: s.lower()), but then you cause two new allocations per comparison, plus burden the garbage-collector with the duplicated (lowered) strings. Each such memory-management noise is orders-of-magnitude slower than simple string comparison. Now, with an in-place stricmp()-like function, you do: theList.sort(cmp=stricmp) and it is as fast and as memory-friendly as theList.sort(). You are happy again. The problem is any Python-based case-insensitive comparison involves implicit string duplications, so I was expecting to find a C-based comparisons (maybe in module string). Could not find anything like that, hence the question here. (Hope this clarifies the question).
16
52
false
python,string,case-insensitive
2008-09-15T12:57:00.000
62,567
Ignore case in Python strings
For occasional or even repeated comparisons, a few extra string objects shouldn't matter as long as this won't happen in the innermost loop of your core code or you don't have enough data to actually notice the performance impact. See if you do: doing things in a "stupid" way is much less stupid if you also do it less. If you seriously want to keep comparing lots and lots of text case-insensitively you could somehow keep the lowercase versions of the strings at hand to avoid finalization and re-creation, or normalize the whole data set into lowercase. This of course depends on the size of the data set. If there are a relatively few needles and a large haystack, replacing the needles with compiled regexp objects is one solution. If It's hard to say without seeing a concrete example.
0
0
0
109,190
65,834
4
What is the easiest way to compare strings in Python, ignoring case? Of course one can do (str1.lower() <= str2.lower()), etc., but this created two additional temporary strings (with the obvious alloc/g-c overheads). I guess I'm looking for an equivalent to C's stricmp(). [Some more context requested, so I'll demonstrate with a trivial example:] Suppose you want to sort a looong list of strings. You simply do theList.sort(). This is O(n * log(n)) string comparisons and no memory management (since all strings and list elements are some sort of smart pointers). You are happy. Now, you want to do the same, but ignore the case (let's simplify and say all strings are ascii, so locale issues can be ignored). You can do theList.sort(key=lambda s: s.lower()), but then you cause two new allocations per comparison, plus burden the garbage-collector with the duplicated (lowered) strings. Each such memory-management noise is orders-of-magnitude slower than simple string comparison. Now, with an in-place stricmp()-like function, you do: theList.sort(cmp=stricmp) and it is as fast and as memory-friendly as theList.sort(). You are happy again. The problem is any Python-based case-insensitive comparison involves implicit string duplications, so I was expecting to find a C-based comparisons (maybe in module string). Could not find anything like that, hence the question here. (Hope this clarifies the question).
16
52
false
python,string,case-insensitive
2008-09-15T12:57:00.000
62,567
Ignore case in Python strings
You could translate each string to lowercase once --- lazily only when you need it, or as a prepass to the sort if you know you'll be sorting the entire collection of strings. There are several ways to attach this comparison key to the actual data being sorted, but these techniques should be addressed in a separate issue. Note that this technique can be used not only to handle upper/lower case issues, but for other types of sorting such as locale specific sorting, or "Library-style" title sorting that ignores leading articles and otherwise normalizes the data before sorting it.
0
0
1
15,517
63,216
16
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
22
10
false
python
2008-09-15T13:55:00.000
63,086
Is there a way around coding in Python without the tab, indent & whitespace criteria?
I agree with justin and others -- pick a good editor and use spaces rather than tabs for indentation and the whitespace thing becomes a non-issue. I only recently started using Python, and while I thought the whitespace issue would be a real annoyance it turns out to not be the case. For the record I'm using emacs though I'm sure there are other editors out there that do an equally fine job. If you're really dead-set against it, you can always pass your scripts through a pre-processor but that's a bad idea on many levels. If you're going to learn a language, embrace the features of that language rather than try to work around them. Otherwise, what's the point of learning a new language?
0.01818
2
0
15,517
63,119
16
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
22
10
false
python
2008-09-15T13:55:00.000
63,086
Is there a way around coding in Python without the tab, indent & whitespace criteria?
All of the whitespace issues I had when I was starting Python were the result mixing tabs and spaces. Once I configured everything to just use one or the other, I stopped having problems. In my case I configured UltraEdit & vim to use spaces in place of tabs.
1
6
0
15,517
63,819
16
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
22
10
false
python
2008-09-15T13:55:00.000
63,086
Is there a way around coding in Python without the tab, indent & whitespace criteria?
I find it hard to understand when people flag this as a problem with Python. I took to it immediately and actually find it's one of my favourite 'features' of the language :) In other languages I have two jobs: 1. Fix the braces so the computer can parse my code 2. Fix the indentation so I can parse my code. So in Python I have half as much to worry about ;-) (nb the only time I ever have problem with indendation is when Python code is in a blog and a forum that messes with the white-space but this is happening less and less as the apps get smarter)
0.045423
5
0
15,517
63,095
16
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
22
10
false
python
2008-09-15T13:55:00.000
63,086
Is there a way around coding in Python without the tab, indent & whitespace criteria?
I do not believe so, as Python is a whitespace-delimited language. Perhaps a text editor or IDE with auto-indentation would be of help. What are you currently using?
0.027266
3
1
15,517
63,111
16
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
22
10
false
python
2008-09-15T13:55:00.000
63,086
Is there a way around coding in Python without the tab, indent & whitespace criteria?
No, there isn't. Indentation is syntax for Python. You can: Use tabnanny.py to check your code Use a syntax-aware editor that highlights such mistakes (vi does that, emacs I bet it does, and then, most IDEs do too) (far-fetched) write a preprocessor of your own to convert braces (or whatever block delimiters you love) into indentation
0.027266
3
2
15,517
63,196
16
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
22
10
false
python
2008-09-15T13:55:00.000
63,086
Is there a way around coding in Python without the tab, indent & whitespace criteria?
It's possible to write a pre-processor which takes randomly-indented code with pseudo-python keywords like "endif" and "endwhile" and properly indents things. I had to do this when using python as an "ASP-like" language, because the whole notion of "indentation" gets a bit fuzzy in such an environment. Of course, even with such a thing you really ought to indent sanely, at which point the conveter becomes superfluous.
0.045423
5
0
15,517
68,061
16
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
22
10
false
python
2008-09-15T13:55:00.000
63,086
Is there a way around coding in Python without the tab, indent & whitespace criteria?
Emacs! Seriously, its use of "tab is a command, not a character", is absolutely perfect for python development.
1
10
5
15,517
63,122
16
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
22
10
false
python
2008-09-15T13:55:00.000
63,086
Is there a way around coding in Python without the tab, indent & whitespace criteria?
You should disable tab characters in your editor when you're working with Python (always, actually, IMHO, but especially when you're working with Python). Look for an option like "Use spaces for tabs": any decent editor should have one.
0.027266
3
2
15,517
1,515,244
16
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
22
10
false
python
2008-09-15T13:55:00.000
63,086
Is there a way around coding in Python without the tab, indent & whitespace criteria?
Yes, there is a way. I hate these "no way" answers, there is no way until you discover one. And in that case, whatever it is worth, there is one. I read once about a guy who designed a way to code so that a simple script could re-indent the code properly. I didn't managed to find any links today, though, but I swear I read it. The main tricks are to always use return at the end of a function, always use pass at the end of an if or at the end of a class definition, and always use continue at the end of a while. Of course, any other no-effect instruction would fit the purpose. Then, a simple awk script can take your code and detect the end of block by reading pass/continue/return instructions, and the start of code with if/def/while/... instructions. Of course, because you'll develop your indenting script, you'll see that you don't have to use continue after a return inside the if, because the return will trigger the indent-back mechanism. The same applies for other situations. Just get use to it. If you are diligent, you'll be able to cut/paste and add/remove if and correct the indentations automagically. And incidentally, pasting code from the web will require you to understand a bit of it so that you can adapt it to that "non-classical" setting.
0
0
3
15,517
63,289
16
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
22
10
false
python
2008-09-15T13:55:00.000
63,086
Is there a way around coding in Python without the tab, indent & whitespace criteria?
Getting your indentation to work correctly is going to be important in any language you use. Even though it won't affect the execution of the program in most other languages, incorrect indentation can be very confusing for anyone trying to read your program, so you need to invest the time in figuring out how to configure your editor to align things correctly. Python is pretty liberal in how it lets you indent. You can pick between tabs and spaces (but you really should use spaces) and can pick how many spaces. The only thing it requires is that you are consistent which ultimately is important no matter what language you use.
0.01818
2
2
15,517
63,450
16
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
22
10
false
python
2008-09-15T13:55:00.000
63,086
Is there a way around coding in Python without the tab, indent & whitespace criteria?
I was a bit reluctant to learn Python because of tabbing. However, I almost didn't notice it when I used Vim.
0.009091
1
3
15,517
69,064
16
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
22
10
false
python
2008-09-15T13:55:00.000
63,086
Is there a way around coding in Python without the tab, indent & whitespace criteria?
Check the options of your editor or find an editor/IDE that allows you to convert TABs to spaces. I usually set the options of my editor to substitute the TAB character with 4 spaces, and I never run into any problems.
0
0
2
15,517
64,899
16
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
22
10
false
python
2008-09-15T13:55:00.000
63,086
Is there a way around coding in Python without the tab, indent & whitespace criteria?
In Python, indentation is a semantic element as well as providing visual grouping for readability. Both space and tab can indicate indentation. This is unfortunate, because: The interpretation(s) of a tab varies among editors and IDEs and is often configurable (and often configured). OTOH, some editors are not configurable but apply their own rules for indentation. Different sequences of spaces and tabs may be visually indistinguishable. Cut and pastes can alter whitespace. So, unless you know that a given piece of code will only be modified by yourself with a single tool and an unvarying config, you must avoid tabs for indentation (configure your IDE) and make sure that you are warned if they are introduced (search for tabs in leading whitespace). And you can still expect to be bitten now and then, as long as arbitrary semantics are applied to control characters.
0
0
2
15,517
64,186
16
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
22
10
false
python
2008-09-15T13:55:00.000
63,086
Is there a way around coding in Python without the tab, indent & whitespace criteria?
Many Python IDEs and generally-capable text/source editors can handle the whitespace for you. However, it is best to just "let go" and enjoy the whitespace rules of Python. With some practice, they won't get into your way at all, and you will find they have many merits, the most important of which are: Because of the forced whitespace, Python code is simpler to understand. You will find that as you read code written by others, it is easier to grok than code in, say, Perl or PHP. Whitespace saves you quite a few keystrokes of control characters like { and }, which litter code written in C-like languages. Less {s and }s means, among other things, less RSI and wrist pain. This is not a matter to take lightly.
0
0
3
15,517
1,538,995
16
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
22
10
false
python
2008-09-15T13:55:00.000
63,086
Is there a way around coding in Python without the tab, indent & whitespace criteria?
I'm surprised no one has mentioned IDLE as a good default python editor. Nice syntax colors, handles indents, has intellisense, easy to adjust fonts, and it comes with the default download of python. Heck, I write mostly IronPython, but it's so nice & easy to edit in IDLE and run ipy from a command prompt. Oh, and what is the big deal about whitespace? Most easy to read C or C# is well indented, too, python just enforces a really simple formatting rule.
0.009091
1
3
15,517
63,094
16
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
22
10
false
python
2008-09-15T13:55:00.000
63,086
Is there a way around coding in Python without the tab, indent & whitespace criteria?
No. Indentation-as-grammar is an integral part of the Python language, for better and worse.
1
37
0
1,259
63,794
2
I'm hosting Python script with Python for Delphi components inside my Delphi application. I'd like to create background tasks which keep running by script. Is it possible to create threads which keep running even if the script execution ends (but not the host process, which keeps going on). I've noticed that the program gets stuck if the executing script ends and there is thread running. However if I'll wait until the thread is finished everything goes fine. I'm trying to use "threading" standard module for threads.
3
1
false
python,delphi
2008-09-15T15:00:00.000
63,681
How create threads under Python for Delphi
If a process dies all it's threads die with it, so a solution might be a separate process. See if creating a xmlrpc server might help you, that is a simple solution for interprocess communication.
0
0
0
1,259
63,767
2
I'm hosting Python script with Python for Delphi components inside my Delphi application. I'd like to create background tasks which keep running by script. Is it possible to create threads which keep running even if the script execution ends (but not the host process, which keeps going on). I've noticed that the program gets stuck if the executing script ends and there is thread running. However if I'll wait until the thread is finished everything goes fine. I'm trying to use "threading" standard module for threads.
3
1
false
python,delphi
2008-09-15T15:00:00.000
63,681
How create threads under Python for Delphi
Threads by definition are part of the same process. If you want them to keep running, they need to be forked off into a new process; see os.fork() and friends. You'll probably want the new process to end (via exit() or the like) immediately after spawning the script.
0
0
1
1,205
64,195
2
In Python is there any way to make a class, then make a second version of that class with identical dat,a but which can be changed, then reverted to be the same as the data in the original class? So I would make a class with the numbers 1 to 5 as the data in it, then make a second class with the same names for sections (or very similar). Mess around with the numbers in the second class then with one function then reset them to be the same as in the first class. The only alternative I've found is to make one aggravatingly long class with too many separate pieces of data in it to be readily usable.
6
1
false
python,class
2008-09-15T15:51:00.000
64,141
Classes in Python
Classes don't have values. Objects do. Is what you want basically a class that can reset an instance (object) to a set of default values? How about just providing a reset method, that resets the properties of your object to whatever is the default? I think you should simplify your question, or tell us what you really want to do. It's not at all clear.
0.033321
1
1
1,205
64,206
2
In Python is there any way to make a class, then make a second version of that class with identical dat,a but which can be changed, then reverted to be the same as the data in the original class? So I would make a class with the numbers 1 to 5 as the data in it, then make a second class with the same names for sections (or very similar). Mess around with the numbers in the second class then with one function then reset them to be the same as in the first class. The only alternative I've found is to make one aggravatingly long class with too many separate pieces of data in it to be readily usable.
6
1
false
python,class
2008-09-15T15:51:00.000
64,141
Classes in Python
I think you are confused. You should re-check the meaning of "class" and "instance". I think you are trying to first declare a Instance of a certain Class, and then declare a instance of other Class, use the data from the first one, and then find a way to convert the data in the second instance and use it on the first instance... I recommend that you use operator overloading to assign the data.
0.033321
1
2
120,959
66,818
1
I've trouble setting up Vim (7.1.xxx) for editing Python files (*.py). Indenting seems to be broken (optimal 4 spaces). I've followed some tutorials I found via Google. Still no effect :/ Please help.
7
89
false
python,vim,configuration,spaces
2008-09-15T17:48:00.000
65,076
How do I set up Vim autoindentation properly for editing Python files?
Ensure you are editing the correct configuration file for VIM. Especially if you are using windows, where the file could be named _vimrc instead of .vimrc as on other platforms. In vim type :help vimrc and check your path to the _vimrc/.vimrc file with :echo $HOME :echo $VIM Make sure you are only using one file. If you want to split your configuration into smaller chunks you can source other files from inside your _vimrc file. :help source
0.085505
3
1
2,008
261,520
5
I'm looking to write a programming language for fun, however most of the resource I have seen are for writing a context free language, however I wish to write a language that, like python, uses indentation, which to my understanding means it can't be context free.
12
14
false
python,compiler-construction,programming-languages,interpreter
2008-09-16T00:28:00.000
68,243
What's a good resource for starting to write a programming language, that's not context free?
A context-free grammar is, simply, one that doesn't require a symbol table in order to correctly parse the code. A context-sensitive grammar does. The D programming language is an example of a context free grammar. C++ is a context sensitive one. (For example, is T*x declaring x to be pointer to T, or is it multiplying T by x ? We can only tell by looking up T in the symbol table to see if it is a type or a variable.) Whitespace has nothing to do with it. D uses a context free grammar in order to greatly simplify parsing it, and so that simple tools can parse it (such as syntax highlighting editors).
1
19
4
2,008
315,462
5
I'm looking to write a programming language for fun, however most of the resource I have seen are for writing a context free language, however I wish to write a language that, like python, uses indentation, which to my understanding means it can't be context free.
12
14
false
python,compiler-construction,programming-languages,interpreter
2008-09-16T00:28:00.000
68,243
What's a good resource for starting to write a programming language, that's not context free?
If you're really going to take a whack at language design and implementation, you might want to add the following to your bookshelf: Programming Language Pragmatics, Scott et al. Design Concepts in Programming Languages, Turbak et al. Modern Compiler Design, Grune et al. (I sacrilegiously prefer this to "The Dragon Book" by Aho et al.) Gentler introductions such as: Crenshaw's tutorial (as suggested by @'Jonas Gorauskas' here) The Definitive ANTLR Reference by Parr Martin Fowler's recent work on DSLs You should also consider your implementation language. This is one of those areas where different languages vastly differ in what they facilitate. You should consider languages such as LISP, F# / OCaml, and Gilad Bracha's new language Newspeak.
0.033321
2
1
2,008
69,482
5
I'm looking to write a programming language for fun, however most of the resource I have seen are for writing a context free language, however I wish to write a language that, like python, uses indentation, which to my understanding means it can't be context free.
12
14
false
python,compiler-construction,programming-languages,interpreter
2008-09-16T00:28:00.000
68,243
What's a good resource for starting to write a programming language, that's not context free?
If you've never written a parser before, start with something simple. Parsers are surprisingly subtle, and you can get into all sorts of trouble writing them if you've never studied the structure of programming languages. Reading Aho, Sethi, and Ullman (it's known as "The Dragon Book") is a good plan. Contrary to other contributors, I say you should play with simpler parser generators like Yacc and Bison first, and only when you get burned because you can't do something with that tool should you go on to try to build something with an LL(*) parser like Antlr.
0.016665
1
1
2,008
69,185
5
I'm looking to write a programming language for fun, however most of the resource I have seen are for writing a context free language, however I wish to write a language that, like python, uses indentation, which to my understanding means it can't be context free.
12
14
false
python,compiler-construction,programming-languages,interpreter
2008-09-16T00:28:00.000
68,243
What's a good resource for starting to write a programming language, that's not context free?
Have you read Aho, Sethi, Ullman: "Compilers: Principles, Techniques, and Tools"? It is a classical language reference book. /Allan
0.016665
1
2
2,008
68,575
5
I'm looking to write a programming language for fun, however most of the resource I have seen are for writing a context free language, however I wish to write a language that, like python, uses indentation, which to my understanding means it can't be context free.
12
14
false
python,compiler-construction,programming-languages,interpreter
2008-09-16T00:28:00.000
68,243
What's a good resource for starting to write a programming language, that's not context free?
I would recommend that you write your parser by hand, in which case having significant whitespace should not present any real problems. The main problem with using a parser generator is that it is difficult to get good error recovery in the parser. If you plan on implementing an IDE for your language, then having good error recovery is important for getting things like Intellisence to work. Intellisence always works on incomplete syntactic constructs, and the better the parser is at figuring out what construct the user is trying to type, the better an intellisence experience you can deliver. If you write a hand-written top-down parser, you can pretty much implement what ever rules you want, where ever you want to. This is what makes it easy to provide error recovery. It will also make it trivial for you to implement significant whitespace. You can simply store what the current indentation level is in a variable inside your parser class, and can stop parsing blocks when you encounter a token on a new line that has a column position that is less than the current indentation level. Also, chances are that you are going to run into ambiguities in your grammar. Most “production” languages in wide use have syntactic ambiguities. A good example is generics in C# (there are ambiguities around "<" in an expression context, it can be either a "less-than" operator, or the start of a "generic argument list"). In a hand-written parser solving ambiguities like that are trivial. You can just add a little bit of non-determinism where you need it with relatively little impact on the rest of the parser, Furthermore, because you are designing the language yourself, you should assume it's design is going to evolve rapidly (for some languages with standards committees, like C++ this is not the case). Making changes to automatically generated parsers to either handle ambiguities, or evolve the language, may require you to do significant refactoring of the grammar, which can be both irritating and time consuming. Changes to hand written parsers, particularly for top-down parsers, are usually pretty localized. I would say that parser generators are only a good choice if: You never plan on writing an IDE ever, The language has really simple syntax, or You need a parser extremely quickly, and are ok with a bad user experience
0.016665
1
2
113,174
68,638
4
Is there any performance difference between tuples and lists when it comes to instantiation and retrieval of elements?
9
284
false
python,performance,list,tuples,python-internals
2008-09-16T01:43:00.000
68,630
Are tuples more efficient than lists in Python?
Tuples should be slightly more efficient and because of that, faster, than lists because they are immutable.
0.088656
4
3
113,174
71,295
4
Is there any performance difference between tuples and lists when it comes to instantiation and retrieval of elements?
9
284
false
python,performance,list,tuples,python-internals
2008-09-16T01:43:00.000
68,630
Are tuples more efficient than lists in Python?
You should also consider the array module in the standard library if all the items in your list or tuple are of the same C type. It will take less memory and can be faster.
1
9
-1
113,174
53,385,158
4
Is there any performance difference between tuples and lists when it comes to instantiation and retrieval of elements?
9
284
false
python,performance,list,tuples,python-internals
2008-09-16T01:43:00.000
68,630
Are tuples more efficient than lists in Python?
The main reason for Tuple to be very efficient in reading is because it's immutable. Why immutable objects are easy to read? The reason is tuples can be stored in the memory cache, unlike lists. The program always read from the lists memory location as it is mutable (can change any time).
-1
-6
5
113,174
70,968
4
Is there any performance difference between tuples and lists when it comes to instantiation and retrieval of elements?
9
284
false
python,performance,list,tuples,python-internals
2008-09-16T01:43:00.000
68,630
Are tuples more efficient than lists in Python?
Tuples, being immutable, are more memory efficient; lists, for speed efficiency, overallocate memory in order to allow appends without constant reallocs. So, if you want to iterate through a constant sequence of values in your code (eg for direction in 'up', 'right', 'down', 'left':), tuples are preferred, since such tuples are pre-calculated in compile time. Read-access speeds should be the same (they are both stored as contiguous arrays in the memory). But, alist.append(item) is much preferred to atuple+= (item,) when you deal with mutable data. Remember, tuples are intended to be treated as records without field names.
1
41
2
1,501,442
61,080,153
2
How do I create static class variables or methods in Python?
26
2,352
false
python,class,oop,static,class-variables
2008-09-16T01:46:00.000
68,645
Static class variables and methods in Python
So this is probably a hack, but I've been using eval(str) to obtain an static object, kind of a contradiction, in python 3. There is an Records.py file that has nothing but class objects defined with static methods and constructors that save some arguments. Then from another .py file I import Records but i need to dynamically select each object and then instantiate it on demand according to the type of data being read in. So where object_name = 'RecordOne' or the class name, I call cur_type = eval(object_name) and then to instantiate it you do cur_inst = cur_type(args) However before you instantiate you can call static methods from cur_type.getName() for example, kind of like abstract base class implementation or whatever the goal is. However in the backend, it's probably instantiated in python and is not truly static, because eval is returning an object....which must have been instantiated....that gives static like behavior.
0.023073
3
3
1,501,442
79,840
2
How do I create static class variables or methods in Python?
26
2,352
false
python,class,oop,static,class-variables
2008-09-16T01:46:00.000
68,645
Static class variables and methods in Python
To avoid any potential confusion, I would like to contrast static variables and immutable objects. Some primitive object types like integers, floats, strings, and touples are immutable in Python. This means that the object that is referred to by a given name cannot change if it is of one of the aforementioned object types. The name can be reassigned to a different object, but the object itself may not be changed. Making a variable static takes this a step further by disallowing the variable name to point to any object but that to which it currently points. (Note: this is a general software concept and not specific to Python; please see others' posts for information about implementing statics in Python).
0.038443
5
4
1,321,249
70,869
1
How do I have a Python script that a) can accept user input and how do I make it b) read in arguments if run from the command line?
12
591
false
python,input,command-line-arguments
2008-09-16T09:44:00.000
70,797
How to prompt for user input and read command-line arguments
Careful not to use the input function, unless you know what you're doing. Unlike raw_input, input will accept any python expression, so it's kinda like eval
1
19
1
5,445
76,404
2
I am trying to use the import random statement in python, but it doesn't appear to have any methods in it to use. Am I missing something?
8
9
false
python
2008-09-16T16:49:00.000
74,430
Random in python 2.5 not working?
If the script you are trying to run is itself called random.py, then you would have a naming conflict. Choose a different name for your script.
0.024995
1
2
5,445
78,304
2
I am trying to use the import random statement in python, but it doesn't appear to have any methods in it to use. Am I missing something?
8
9
false
python
2008-09-16T16:49:00.000
74,430
Random in python 2.5 not working?
This is happening because you have a random.py file in the python search path, most likely the current directory. Python is searching for modules using sys.path, which normally includes the current directory before the standard site-packages, which contains the expected random.py. This is expected to be fixed in Python 3.0, so that you can't import modules from the current directory without using a special import syntax. Just remove the random.py + random.pyc in the directory you're running python from and it'll work fine.
0.07486
3
5
45,412
77,222
2
What method do I call to get the name of a class?
5
50
false
python
2008-09-16T18:26:00.000
75,440
How do I get the string with name of a class?
In [8]: str('2'.__class__) Out[8]: "<type 'str'>" In [9]: str(len.__class__) Out[9]: "<type 'builtin_function_or_method'>" In [10]: str(4.6.__class__) Out[10]: "<type 'float'>" Or, as was pointed out before, In [11]: 4.6.__class__.__name__ Out[11]: 'float'
0.039979
1
1
45,412
75,456
2
What method do I call to get the name of a class?
5
50
false
python
2008-09-16T18:26:00.000
75,440
How do I get the string with name of a class?
It's not a method, it's a field. The field is called __name__. class.__name__ will give the name of the class as a string. object.__class__.__name__ will give the name of the class of an object.
1
43
2
9,282
186,583
3
I have written a lot of code in Python, and I am very used to the syntax, object structure, and so forth of Python because of it. What is the best online guide or resource site to provide me with the basics, as well as a comparison or lookup guide with equivalent functions/features in VBA versus Python. For example, I am having trouble equating a simple List in Python to VBA code. I am also have issues with data structures, such as dictionaries, and so forth. What resources or tutorials are available that will provide me with a guide to porting python functionality to VBA, or just adapting to the VBA syntax from a strong OOP language background?
8
21
false
python,vba,porting
2008-09-16T20:48:00.000
76,882
Resources for Python Programmer
VBA is quite different from Python, so you should read at least the "Microsoft Visual Basic Help" as provided by the application you are going to use (Excel, Access…). Generally speaking, VBA has the equivalent of Python modules; they're called "Libraries", and they are not as easy to create as Python modules. I mention them because Libraries will provide you with higher-level types that you can use. As a start-up nudge, there are two types that can be substituted for list and dict. list VBA has the type Collection. It's available by default (it's in the library VBA). So you just do a dim alist as New Collection and from then on, you can use its methods/properties: .Add(item) ( list.append(item) ), .Count ( len(list) ), .Item(i) ( list[i] ) and .Remove(i) ( del list[i] ). Very primitive, but it's there. You can also use the VBA Array type, which like python arrays are lists of same-type items, and unlike python arrays, you need to do ReDim to change their size (i.e. you can't just append and remove items) dict To have a dictionary-like object, you should add the Scripting library to your VBA project¹. Afterwards, you can Dim adict As New Dictionary and then use its properties/methods: .Add(key, item) ( dict[key] = item ), .Exists(key) ( dict.has_key[key] ), .Items() ( dict.values() ), .Keys() ( dict.keys() ), and others which you will find in the Object Browser². ¹ Open VBA editor (Alt+F11). Go to Tools→References, and check the "Microsoft Scripting Runtime" in the list. ² To see the Object Browser, in VBA editor press F2 (or View→Object Browser).
1
25
2
9,282
189,311
3
I have written a lot of code in Python, and I am very used to the syntax, object structure, and so forth of Python because of it. What is the best online guide or resource site to provide me with the basics, as well as a comparison or lookup guide with equivalent functions/features in VBA versus Python. For example, I am having trouble equating a simple List in Python to VBA code. I am also have issues with data structures, such as dictionaries, and so forth. What resources or tutorials are available that will provide me with a guide to porting python functionality to VBA, or just adapting to the VBA syntax from a strong OOP language background?
8
21
false
python,vba,porting
2008-09-16T20:48:00.000
76,882
Resources for Python Programmer
While I'm not a Python programmer, you might be able to run VSTO with Iron Python and Visual Studio. At least that way, you won't have to learn VBA syntax.
0.049958
2
5
9,282
76,989
3
I have written a lot of code in Python, and I am very used to the syntax, object structure, and so forth of Python because of it. What is the best online guide or resource site to provide me with the basics, as well as a comparison or lookup guide with equivalent functions/features in VBA versus Python. For example, I am having trouble equating a simple List in Python to VBA code. I am also have issues with data structures, such as dictionaries, and so forth. What resources or tutorials are available that will provide me with a guide to porting python functionality to VBA, or just adapting to the VBA syntax from a strong OOP language background?
8
21
false
python,vba,porting
2008-09-16T20:48:00.000
76,882
Resources for Python Programmer
VBA as in what was implemented as part of Office 2000, 2003 and VB6 have been deprecated in favor of .Net technologies. Unless you are maintaining old code stick to python or maybe even go with IronPython for .Net. If you go IronPython, you may have to write some C#/VB.Net helper classes here and there when working with various COM objects such as ones in Office but otherwise it is supposed to be pretty functional and nice. Just about all of the Python goodness is over in IronPython. If you are just doing some COM scripting take a look at what ActiveState puts out. I've used it in the past to do some COM work. Specifically using Python as an Active Scripting language (classic ASP).
0.049958
2
6
62,544
77,925
4
Why is it bad to name a variable id in Python?
9
187
false
python
2008-09-16T21:50:00.000
77,552
'id' is a bad variable name in Python
I might say something unpopular here: id() is a rather specialized built-in function that is rarely used in business logic. Therefore I don't see a problem in using it as a variable name in a tight and well-written function, where it's clear that id doesn't mean the built-in function.
1
56
-1
62,544
78,433
4
Why is it bad to name a variable id in Python?
9
187
false
python
2008-09-16T21:50:00.000
77,552
'id' is a bad variable name in Python
Because python is a dynamic language, it's not usually a good idea to give a variable and a function the same name. id() is a function in python, so it's recommend not to use a variable named id. Bearing that in mind, that applies to all functions that you might use... a variable shouldn't have the same name as a function.
-1
-6
-1
62,544
77,563
4
Why is it bad to name a variable id in Python?
9
187
false
python
2008-09-16T21:50:00.000
77,552
'id' is a bad variable name in Python
Because it's the name of a builtin function.
-0.044415
-2
3
62,544
77,600
4
Why is it bad to name a variable id in Python?
9
187
false
python
2008-09-16T21:50:00.000
77,552
'id' is a bad variable name in Python
It's bad to name any variable after a built in function. One of the reasons is because it can be confusing to a reader that doesn't know the name is overridden.
0.110656
5
4
1,803
86,173
3
I am relatively new to Python, and I have always used the standard cpython (v2.5) implementation. I've been wondering about the other implementations though, particularly Jython and IronPython. What makes them better? What makes them worse? What other implementations are there? I guess what I'm looking for is a summary and list of pros and cons for each implementation.
6
9
false
python
2008-09-17T18:25:00.000
86,134
What are the pros and cons of the various Python implementations?
Jython and IronPython are useful if you have an overriding need to interface with existing libraries written in a different platform, like if you have 100,000 lines of Java and you just want to write a 20-line Python script. Not particularly useful for anything else, in my opinion, because they are perpetually a few versions behind CPython due to community inertia. Stackless is interesting because it has support for green threads, continuations, etc. Sort of an Erlang-lite. PyPy is an experimental interpreter/compiler that may one day supplant CPython, but for now is more of a testbed for new ideas.
1
15
1
1,803
86,427
3
I am relatively new to Python, and I have always used the standard cpython (v2.5) implementation. I've been wondering about the other implementations though, particularly Jython and IronPython. What makes them better? What makes them worse? What other implementations are there? I guess what I'm looking for is a summary and list of pros and cons for each implementation.
6
9
false
python
2008-09-17T18:25:00.000
86,134
What are the pros and cons of the various Python implementations?
IronPython and Jython use the runtime environment for .NET or Java and with that comes Just In Time compilation and a garbage collector different from the original CPython. They might be also faster than CPython thanks to the JIT, but I don't know that for sure. A downside in using Jython or IronPython is that you cannot use native C modules, they can be only used in CPython.
0.033321
1
1
1,803
86,172
3
I am relatively new to Python, and I have always used the standard cpython (v2.5) implementation. I've been wondering about the other implementations though, particularly Jython and IronPython. What makes them better? What makes them worse? What other implementations are there? I guess what I'm looking for is a summary and list of pros and cons for each implementation.
6
9
false
python
2008-09-17T18:25:00.000
86,134
What are the pros and cons of the various Python implementations?
Pros: Access to the libraries available for JVM or CLR. Cons: Both naturally lag behind CPython in terms of features.
0.033321
1
3
75,677
88,661
2
If I have this string: 2+24*48/32 what is the most efficient approach for creating this list: ['2', '+', '24', '*', '48', '/', '32']
12
36
false
python,string,list,split
2008-09-17T23:17:00.000
88,613
How do I split a string into a list?
s = "2+24*48/32" p = re.compile(r'(\W+)') p.split(s)
0.083141
5
0
75,677
3,517,872
2
If I have this string: 2+24*48/32 what is the most efficient approach for creating this list: ['2', '+', '24', '*', '48', '/', '32']
12
36
false
python,string,list,split
2008-09-17T23:17:00.000
88,613
How do I split a string into a list?
This doesn't answer the question exactly, but I believe it solves what you're trying to achieve. I would add it as a comment, but I don't have permission to do so yet. I personally would take advantage of Python's maths functionality directly with exec: expression = "2+24*48/32" exec "result = " + expression print result 38
0
0
-1
148,492
89,940
1
I know how to do this if I iterate through all of the characters in the string but I am looking for a more elegant method.
11
95
false
python,regex,string
2008-09-18T04:04:00.000
89,909
How do I verify that a string only contains letters, numbers, underscores and dashes?
You could always use a list comprehension and check the results with all, it would be a little less resource intensive than using a regex: all([c in string.letters + string.digits + ["_", "-"] for c in mystring])
-0.01818
-1
5
464
91,301
1
I'm a little confused about how the standard library will behave now that Python (from 3.0) is unicode-based. Will modules such as CGI and urllib use unicode strings or will they use the new 'bytes' type and just provide encoded data?
3
12
true
python,unicode,string,cgi,python-3.x
2008-09-18T09:29:00.000
91,205
Will everything in the standard library treat strings as unicode in Python 3.0?
Logically a lot of things like MIME-encoded mail messages, URLs, XML documents, and so on should be returned as bytes not strings. This could cause some consternation as the libraries start to be nailed down for Python 3 and people discover that they have to be more aware of the bytes/string conversions than they were for str/unicode ...
1.2
12
2
4,581
92,254
3
I've gotten to grips with the basics of Python and I've got a small holiday which I want to use some of to learn a little more Python. The problem is that I have no idea what to learn or where to start. I'm primarily web development but in this case I don't know how much difference it will make.
10
14
false
python
2008-09-18T12:51:00.000
92,230
Python, beyond the basics
I'd suggest writing a non-trivial webapp using either Django or Pylons, something that does some number crunching. No better way to learn a new language than commiting yourself to a problem and learning as you go!
0.019997
1
1
4,581
92,691
3
I've gotten to grips with the basics of Python and I've got a small holiday which I want to use some of to learn a little more Python. The problem is that I have no idea what to learn or where to start. I'm primarily web development but in this case I don't know how much difference it will make.
10
14
false
python
2008-09-18T12:51:00.000
92,230
Python, beyond the basics
The Python Cookbook is absolutely essential if you want to master idiomatic Python. Besides, that's the book that made me fall in love with the language.
0.039979
2
1
4,581
181,078
3
I've gotten to grips with the basics of Python and I've got a small holiday which I want to use some of to learn a little more Python. The problem is that I have no idea what to learn or where to start. I'm primarily web development but in this case I don't know how much difference it will make.
10
14
false
python
2008-09-18T12:51:00.000
92,230
Python, beyond the basics
Search "Alex Martelli", "Alex Martelli patterns" and "Thomas Wouters" on Google video. There's plenty of interesting talks on advanced Python, design patterns in Python, and so on.
0.019997
1
5
295,850
92,953
5
In Python for *nix, does time.sleep() block the thread or the process?
7
402
false
python,multithreading,time,sleep,python-internals
2008-09-18T14:16:00.000
92,928
time.sleep -- sleeps thread or process?
Just the thread.
1
38
2
295,850
32,216,136
5
In Python for *nix, does time.sleep() block the thread or the process?
7
402
false
python,multithreading,time,sleep,python-internals
2008-09-18T14:16:00.000
92,928
time.sleep -- sleeps thread or process?
Only the thread unless your process has a single thread.
0.113791
4
4
295,850
93,069
5
In Python for *nix, does time.sleep() block the thread or the process?
7
402
false
python,multithreading,time,sleep,python-internals
2008-09-18T14:16:00.000
92,928
time.sleep -- sleeps thread or process?
The thread will block, but the process is still alive. In a single threaded application, this means everything is blocked while you sleep. In a multithreaded application, only the thread you explicitly 'sleep' will block and the other threads still run within the process.
1
18
1
295,850
59,821,834
5
In Python for *nix, does time.sleep() block the thread or the process?
7
402
false
python,multithreading,time,sleep,python-internals
2008-09-18T14:16:00.000
92,928
time.sleep -- sleeps thread or process?
it blocks a thread if it is executed in the same thread not if it is executed from the main code
0.028564
1
2
295,850
40,313,394
5
In Python for *nix, does time.sleep() block the thread or the process?
7
402
false
python,multithreading,time,sleep,python-internals
2008-09-18T14:16:00.000
92,928
time.sleep -- sleeps thread or process?
Process is not runnable by itself. In regard to execution, process is just a container for threads. Meaning you can't pause the process at all. It is simply not applicable to process.
0.085505
3
4
2,131
97,635
1
When using PyWin I can easily load a python file into a fresh interactive shell and I find this quite handy for prototyping and other exploratory tasks. I would like to use Komodo as my python editor, but I haven't found a replacement for PyWin's ability to restart the shell and reload the current module. How can I do this in Komodo? It is also very important to me that when I reload I get a fresh shell. I would prefer it if my previous interactions are in the shell history, but it is more important to me that the memory be isolated from the previous versions and attempts.
1
2
true
python,shell,interpreter,komodo
2008-09-18T22:09:00.000
97,513
How to load a python module into a fresh interactive shell in Komodo?
I use Komodo Edit, which might be a little less sophisticated than full Komodo. I create a "New Command" with %(python) -i %f as the text of the command. I have this run in a "New Console". I usually have the starting directory as %p, the top of the project directory. The -i option runs the file and drops into interactive Python.
1.2
5
3
1,011,070
59,818,321
4
What are metaclasses? What are they used for?
24
6,790
false
python,oop,metaclass,python-class,python-datamodel
2008-09-19T06:10:00.000
100,003
What are metaclasses in Python?
A class, in Python, is an object, and just like any other object, it is an instance of "something". This "something" is what is termed as a Metaclass. This metaclass is a special type of class that creates other class's objects. Hence, metaclass is responsible for making new classes. This allows the programmer to customize the way classes are generated. To create a metaclass, overriding of new() and init() methods is usually done. new() can be overridden to change the way objects are created, while init() can be overridden to change the way of initializing the object. Metaclass can be created by a number of ways. One of the ways is to use type() function. type() function, when called with 3 parameters, creates a metaclass. The parameters are :- Class Name Tuple having base classes inherited by class A dictionary having all class methods and class variables Another way of creating a metaclass comprises of 'metaclass' keyword. Define the metaclass as a simple class. In the parameters of inherited class, pass metaclass=metaclass_name Metaclass can be specifically used in the following situations :- when a particular effect has to be applied to all the subclasses Automatic change of class (on creation) is required By API developers
1
14
3
1,011,070
67,201,732
4
What are metaclasses? What are they used for?
24
6,790
false
python,oop,metaclass,python-class,python-datamodel
2008-09-19T06:10:00.000
100,003
What are metaclasses in Python?
In Python, a metaclass is a subclass of a subclass that determines how a subclass behaves. A class is an instance of another metaclass. In Python, a class specifies how the class's instance will behave. Since metaclasses are in charge of class generation, you can write your own custom metaclasses to change how classes are created by performing additional actions or injecting code. Custom metaclasses aren't always important, but they can be.
1
9
4
1,011,070
68,354,618
4
What are metaclasses? What are they used for?
24
6,790
false
python,oop,metaclass,python-class,python-datamodel
2008-09-19T06:10:00.000
100,003
What are metaclasses in Python?
I saw an interesting use case for metaclasses in a package called classutilities. It checks if all class variables are in upper case format (it is convenient to have unified logic for configuration classes), and checks if there are no instance level methods in class. Another interesting example for metaclases was deactivation of unittests based on complex conditions (checking values of multiple environmental variables).
1
9
4
1,011,070
56,945,952
4
What are metaclasses? What are they used for?
24
6,790
false
python,oop,metaclass,python-class,python-datamodel
2008-09-19T06:10:00.000
100,003
What are metaclasses in Python?
In object-oriented programming, a metaclass is a class whose instances are classes. Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain class and their instances The term metaclass simply means something used to create classes. In other words, it is the class of a class. The metaclass is used to create the class so like the object being an instance of a class, a class is an instance of a metaclass. In python classes are also considered objects.
1
16
0
13,411
101,623
2
I need to write a module to detect similar documents. I have read many papers of fingerprints of documents techniques and others, but I do not know how to write code or implement such a solution. The algorithm should work for Chinese, Japanese, English and German language or be language independent. How can I accomplish this?
10
10
false
python,algorithm,diff
2008-09-19T12:47:00.000
101,569
Algorithm to detect similar documents in python script
You need to make your question more concrete. If you've already read the fingerprinting papers, you already know the principles at work, so describing common approaches here would not be beneficial. If you haven't, you should also check out papers on "duplicate detection" and various web spam detection related papers that have come out of Stanford, Google, Yahoo, and MS in recent years. Are you having specific problems with coding the described algorithms? Trouble getting started? The first thing I'd probably do is separate the tokenization (the process of extracting "words" or other sensible sequences) from the duplicate detection logic, so that it is easy to plug in different parsers for different languages and keep the duplicate detection piece the same.
0.059928
3