Talk:Python (programming language)

This is an old revision of this page, as edited by Antaeus Feldspar (talk | contribs) at 17:04, 22 September 2004 (another alternative). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Python is described in "it transparently supports bignum arithmetic". "bignum" is defined as encompasing both arbitrarily large integers and rational numbers. Python does not yet provide for rational number arithmetic.

I don't believe Python supports Functional programming, as in Prolog or somesuch. It supports function-based programming... meaning that your program is just composed of functions not organized into classes and objects. I have gotten the two confused in the past, and I suspect that this is what happened here.

Or does Python really support functional programming? Is there some add-on package that allows this? I wasn't aware of one. -- ansible

Yes, Python really does have some built-in functional programming features such as "map()", "filter()", "reduce()" and a somewhat limited "lambda", although I've read that Guido van Rossum now regrets including these features in the language. As a more readable and "Pythonic" alternative, "list comprehensions" were more recently introduced into the language and can often be used instead of the older functional programming constructs.

I for one do not believe very much in language purity, and I say that as someone who admires Scheme. Fiddly distinctions such as "object-oriented" vs. "object-based", or "functional" vs. "function-based" vs. "pure-functional" are, simply put, religious disputes. As such, they may merit being mentioned in Wikipedia (as do real religious disputes) but should not be taken too seriously.
I consider functional programming, like object-oriented programming, to be chiefly a style of programming -- i.e. something that programmers do, not something that languages do. A language can allow a style of programming, or can even attempt to mandate its use (e.g. Scheme; Java), but it is the program which has a style not the language. It is in this light which I wrote in the subject article that "Object orientation and structured programming are supported, as well as functional programming."
(It is possible to write an imperative-style program in the "functional language" Scheme using begin and do, or an object-oriented program in the "structured language" ANSI C. It isn't possible to write a functional program in Pascal, though, since you can't pass around functions.)
Of course Python does not attempt to be a "pure-functional language" i.e. a language which requires programmers to use functional constructs. Nevertheless, it still permits the style in the large, by (for instance) permitting functions as values, generators, continuations, etc. --FOo
-------
"List comprehensions" may be more Pythonic, but how are they more readable? I find map, filter, and co. to be extremely readable and intuitive, much more so than the new list comprehensions. Unfortunately, GvR doesn't agree with me on that...

I do not think Python is 'strongly influenced' by Perl. In fact the design philosophy is diametrically opposite in some senses.pasokan

Let's see -- Python's an interpreted/bytecode-compiled language, with mixed type arrays, garbage collection, hashes/dicts as a basic type, a big standard library with support for Unix calls ... it has a lot in common with Perl. A large portion of the Python user base are "converts" from Perl. Python also self-consciously eschews Perl-like syntax, which is a different way of being "influenced" by Perl! --FOo

I'm not sure if we should say that Python is strongly typed. It provides a lot of implicit conversions (eg, between numeric types, between string types) and makes common use of functions accepting any one of a group of related types (any sequence, or any 'file-like object'). It doesn't make it easy to derive a new type from an old one without making it compatible (eg, you can't easily make a new integer type which isn't comparable with the standard Integer). Matthew Woodcraft

This is true. Python has a large number of distinct types (integers, floats, long integers, bignums, complex, e.g.) but converts implicitly among them all. I suppose you could say that Python has a complicated system of types, but isn't "strongly typed" ... especially since a lot of folks read "strongly" as "statically". --FOo

Hfastedge -- Please take more care when editing this article. Your point that Python is dynamically typed has already been integrated with the article and does not need to be repeated. A few of your other points were erroneous or misleading, which is why they were removed. Please take the time to understand this and do not simply add them again.

Your statement that Python programs are "interpreted, not compiled" is simply false. Python -- like Java and Perl -- is a bytecode-compiled language. Compilation is implicit before execution of uncompiled scripts, which is why a new Python user might not have noticed its existence. However, it's there; it's how Python works; and yes, compiler errors are distinct from runtime errors. Compiled Python files have the extension .pyc, or .pyo if optimized. If you've never seen one, you haven't used Python for very long.

Python is a dynamically typed language, because that is how it is designed. It is neither a mistake nor an accident. Talking about this as if it is something suitable to "change" with a "paradigm shift" is not talking about Python, it is talking about your opinion of dynamic vs. static typing.

Your claims regarding "massive projects" vs. "rapid prototyping" are ill-advised in the light of the many massive projects that have been written quite successfully in dynamically typed languages (including Python). If you would like to discuss advantages and disadvantages that critics perceive in static and dynamic type-systems, you might want to do so on the pages for those subjects.

In addition, I suggest strongly that you study the writing style of other Wikipedia articles, as it will help you to integrate your work more fully with an article. You might particularly want to watch your use of links -- introducing a link with "here is:" is rough anywhere on the Web. Namespaces are not created lightly. Also, needless indentation turning a Wiki document into a piece of flat text are not desirable. --FOo


substantively?. Ok here: about the above stuff, theres no need for me to reply as I agree more of less with your changes. Although .pyc are simply compiled to bytecode, and python barely does any kind of optimization, so you really like to toot your own horn.
re the new, more relevant stuff, I feel that unit testing should be mentioned directly with the explanation of pythons type system, and that some points of the culture list should be included on the page.

Hfastedge 09:05 18 Jun 2003 (UTC)




A fine general article as the the features of Python, but (unless I'm to tired to notice it) there doesn't seem to be a mention of the uses of Python as a glue language, either with C/C++ using a SWIG interface or with Java in the form of Jython. The stuff about Python vs. Java, whilst being a fair enough comparison, maybe misleads into thinking that their use is in direct competition, whereas they can play complementary roles in development. I'd be tempted to make these comments but I'm (a) rather too new to wikipedia to chance it and (b) way too tired at the moment. But I think this stuff is worth mentioning. sobekhotep


Inheritance

From what I read, Python has inheritance but no access control (public, private,...). Can someone ellaborate on OOP in Python? -- Error

Python has inheritance, including multiple inheritance. It has limited support for private variables using name mangling. See the "Classes" section of the tutorial for details.
From the tutorial: As is true for modules, classes in Python do not put an absolute barrier between definition and user, but rather rely on the politeness of the user not to "break into the definition."
OOP doctrines such as the use of accessor methods to read data members are not enforced in Python. Just as Python offers functional programming constructs but does not attempt to demand referential transparency (contra Scheme), it offers (and extensively uses!) its object system but does not demand OOP behavior (contra Java). --FOo 04:58, 2 Sep 2003 (UTC)

Collection types

Python 2.3 now comes with an un-ordered collection type, the set. I too will refrain from adding to the Python Wiki page as I cannot match the high standard of the article. - Paddy3118


Added link to Aspect oriented programming for the types of programming styles that Python offers. See this article or this google search for support. Hyakugei

Are there enough users who like both to mention them in this article?

Now this is a beautifully written page with very high technical value. I am afraid though that there is a little bit of personal oppinion in the philosophy section (and that seems almost normal :) By judging the comments my point of view was confirmed. For what seems to be the technical influence between Perl and Python there is IMHO more than enough evidence to claim a relationship. When it comes to the point of view of the users I think it might be a good idea to outline that there are two groups of people, myself being part of the second one, and I would love if this second group could be mentioned in this article. My idea would be to write something as follows:

"It is interesting to notice that given the aforementioned similarities [between Perl and Python] there are some who consider them very much different in style and quality. Some others like both languages for their qualities and would rather prefer to see the two languages evolve together".

But great work, really great work.

Robert Dober robert.dober@free.fr 195.167.202.147 16:39, 1 Jun 2004 (UTC)

Python History

Hi, Pythoners. Over at object oriented programming, I wrote the following:

Python and Ruby are relatively recent languages that were built from the ground up with OOP...

I have recently read some conflicting statements about object based functionality only being added in version 2.2. Over at python.org, I was't able to find anything that specifically mentioned whether or not object orientation was in the language from the beginning. Would anyone know? Thanks. AdmN 21:16, 26 Aug 2004 (UTC)

AFAIK, Python was object oriented from the start, or at least from very early on. However, types (int, str, etc) and user classes were separate things (e.g. you couldn't make a class that inherited from int) until 2.2. Fredrik | talk 21:26, 26 Aug 2004 (UTC)

What about Parrot ?

Shouldn't be Parrot, the common framework for Python and Perl be mentioned ?

Maybe it's an attempt of Perl to devour Python, maybe it's just a joke... However, it received attention of several important programmers. For instance, Perl creator.

Hgfernan 22:38, 4 Sep 2004 (UTC)

Formatting of C examples

An anon user removed all but the outermost braces from the example of the factorial function written in C. I have reverted it back for the time being because I'm not sure the anon's version is correct, or clear, whereas the pre-edit version is both.

Is it correct?

The statement blocks from which the anon removed the braces were blocks of one statement each. Since I have not written C code in a decade, my knowledge is rusty; I believe this may be technically correct, that you may omit the braces from a statement block only if there's only one statement in the block. However, I am not sure on this point.

Is it clear?

Even if the code as the anon edited it is compiler-correct, it is misleading; the entire point of the comparison is that Python uses indentation, as opposed to punctuation, which is what C uses in every case except single-statement blocks, where it becomes optional.

What should we do about it?

In the short term, I have reverted, as previously mentioned. However, in the long term, we should perhaps decide between three alternatives (assuming that omission of braces is in fact legal in C for single-statement blocks):

The code as it was before the anon edit. The disadvantage is that it does not show the one instance in C where a programmer may choose to omit the braces. The advantage is that it more accurately reflects the general practices of C. Since the article is about Python and not C, the disadvantages don't strike me as too significant in this case.

The code as it was after the anon edit. The disadvantage is that it does not show one possible way a C programmer might format their code in the one case where they have a choice. The advantage is that it is more accurate, at least in terms of how a C programmer might format (many programmers choose to format consistently in the style that is always allowed, even when they language offers a choice.)

A different example that requires multiple-statement blocks. The disadvantage is that it would require careful selection of a new example. The advantage is that it would not be an artificial example. This doesn't strike me as too much of an advantage; don't hundreds of computer text books demonstrate basic principles by showing the bubble sort, despite the notorious inefficiency of the bubble sort?

I think on the whole my recommendation is for the first alternative, but if anyone disagrees, please bring up the issue here.

-- Antaeus Feldspar 18:26, 21 Sep 2004 (UTC)

Good practice in C uses braces here, so that a later addition to the then-clause does the right thing. Moreover, the distinction being raised is between punctuated blocks (C) and indented blocks (Python), so braces are needed. I think the code is fine as it aws (with the braces) -- an example using multiple-statement blocks is harmless but I do not think it is necessary.FOo 19:24, 21 Sep 2004 (UTC)
Sorry for the removal of the braces in the first place. I made the change facetiously to try to raise the point that it was a poor example since it was valid without the braces. When I first read the comparison of Python code and C code, it did not clearly communicate to me the difference between the two, because I (as a seasoned C programmer) immediately saw that I could remove those braces. Without the knowledge that Python is indentation based, I (as having no prior knowledge of Python) was momentarily confused since it would be natural to assume (to a C programmer) that perhaps it didn't require braces since it only consisted of one statement. Of course, having read the rest of the text, I realised the point that was being attempted to be made, but isn't the intention of the example to make it obvious? I think that, although the page is about Python, since the comparison is with C, the example should be clear to C programmers what the intented point was. — The anon that made the change 11:23, 22 Sep 2004 (UTC)
I think the whole problem is caused by formatting the C example in the same fashion as the Python version. If you re-write the C version in a single line, converting all whitespace to single spaces, you might get the idea across better. HTH HAND --Phil | Talk 12:33, Sep 22, 2004 (UTC)

Example

I have not made this change in the main article in order that someone more familiar with Python can confirm the correctness of the example. --Phil | Talk 12:48, Sep 22, 2004 (UTC)

Factorial function in C
int factorial(int x) {      
    if (x == 0) {                      
        return 1;                   
    } else {
        return x * factorial(x-1);
    }
}

is syntactically equivalent to:

int factorial(int x) { if (x == 0) { return 1; } else { return x * factorial(x-1); } }
Factorial function in Python
def factorial(x):
    if x == 0:
        return 1
    else:
        return x * factorial(x-1)

is not equivalent to:

def factorial(x): if x == 0: return 1 else: return x * factorial(x-1)


Just to present an alternative for consideration, what about presenting both C examples? Show the single-line version, show the braced version, then compare them with the Python version.
Note that I don't necessarily advocate this alternative, I just suggest it's one of our options. After the point that Fubar brought up, I think it's probably best to show two examples formatted according to best practice for the respective language, even if that causes confusion for the occasional reader who (sorry, anon) doesn't read the article to understand the difference that the examples demonstrate. -- Antaeus Feldspar 17:04, 22 Sep 2004 (UTC)