Quite a few poor and superficial answers in that link. The usual argument that dynamic typing does not scale for large projects is largely unfounded. There are large projects in Lisp, I don't get why people don't use the dynamic card as often there. What I have seen happened on badly engineered large projects is people blaming it on dynamic typing because there is no documentation and no structural integrity in the project. But the people behind those projects would not be able to do much in any language IMO. In a sense, it allows for poor programmers to do more.
The speed issue is a significant one, but not usually for the reasons given. The big drawback of being slow means a lots of its implementation is not written in python.
IDE/debugger is another issue, although some of it is a matter of people not knowing the tool. Especially on bad codebases, even something as simple as pyflakes running in the background is invaluable, and many people don't use this.
What I have seen happened on badly engineered large projects is people blaming it on dynamic typing because there is no documentation and no structural integrity in the project.
This can be a problem in static languages with good type inference, too. When reading code in older static languages like C++, you'll find a bunch of variables and types declared, but powerful and concise languages such as Scala have much less use for intermediate variables, and type inference means that intermediate variables are almost never declared with a type. Field types and method return types often aren't declared either. I don't know if this is a good thing because it forces you to read a lot of code, or if it's a bad thing because it forces you to read many unrelated bits of code when you're trying to get something simple done.
There is no agreed-upon definition for the terms strong and weakly typed. Even then, your categorization are unconventional. Python is typically categorized as strongly typed since silent type coercion does not happen. Also I've never heard Java categorized as weak. You can cast, but there is still a runtime check that the cast is valid.
You should avoid using those words, it merely marks you as somebody without a clue. I'm guessing your grandparent is already lost so there's no point in warning him.
C and C++ are weakly typed because you can do things like
int x = 42;
printf("%c\n", *(char *) &x);
that is, access the underlying bits which represent a value as if they were of some completely different type. The results are deliberately not standardized and vary with your machine. Python doesn't provide any way to do this; at worst you get a TypeError for a conversion that isn't implemented, or AttributeError for a slot that an object doesn't have.
Implicitly parsing strings to produce numbers isn't weak typing, merely reckless and prone to mistakes. Weak typing can corrupt the state of your program so severely that even correct code begins to malfunction.
This, in and of itself, means absolutely nothing. And neither does:
> Implicitly parsing strings to produce numbers isn't weak typing
There is no such thing as a widely accepted, let alone formal, definition for a strong/weak axis, every other guy out there has his own personal definition. What the bloody hell is the point of words for which no two persons share the same definition? No point. None at all. See comment above, it merely marks you as a guy without a clue.
But, python is strongly typed, it just isn't dynamically typed. For example, javascript is a weakly typed language, because things such as "a"+1, and Math.floor("a") are all valid; while they would result in a type error in python.
What makes python seem weakly typed to many people is its implicit variable shadowing. So when you do a="hello", a is a str, and can only be used where a str is expected. However, python lets you do:
>a="hello"
>a=1
at which point a is shadowed to an int.
Dynamic typing has some (not all) of the problems weak typing has, but they are not the same thing
"In a weakly typed language a compiler / interpreter will sometimes change the type of a variable. For example, in some languages (like JavaScript) you can add strings to numbers 'x' + 3 becomes 'x3'. This can be a problem because if you have made a mistake in your program, instead of raising an exception execution will continue but your variables now have wrong and unexpected values. In a strongly typed language (like Python) you can't perform operations inappropriate to the type of the object - attempting to add numbers to strings will fail. Problems like these are easier to diagnose because the exception is raised at the point where the error occurs rather than at some other, potentially far removed, place.
In a statically typed language, the type of variables must be known (and usually declared) at the point at which it is used. Attempting to use it will be an error. In a dynamically typed language, objects still have a type, but it is determined at runtime. You are free to bind names (variables) to different objects with a different type. So long as you only perform operations valid for the type the interpreter doesn't care what type they actually are." [0]
It might help you to do research, ask clarifying questions, or explain why you think someone is wrong; instead of assuming that anyone saying something you disagree with has no idea what they are talking about.
When it comes to that precise subject, I happen to have already done so and to know the count rather well. Stupidity is stupidity, even if it's on the python.org wiki.
> or explain why you think someone is wrong
I did so. Twice if not thrice. There is no well-defined and shared — let alone formal — semantics (and meaning) to the strong/weak "axis", every other person has his own pet definition of it[0] and the definitions are quite often incompatible if not downright opposed.
It is therefore garbage and utterly useless to communication.
> instead of assuming that anyone saying something you disagree with has no idea what they are talking about.
It's not "anyone saying something [I] disagree with", just "anyone using the strong/weak axis as if it actually existed".
[0] From "no memory corruption" to "no possible runtime error" through "no UB", "dynamically typed" and all sorts of takes on "no implicit conversions", usually in order to declare one's pet language is "strong" and somebody else's is "weak". I've seen all of C, C++, Python, Javascript, Perl, Java, Tcl, Lua, Smalltalk or Pascal declared both strong and weak by various (usually different) people.
I suggest you start to prefer learning from people instead of insulting them - you'll do much better in life. At least learn to either be polite or keep quiet.
I've been digging into Clojure and reading Paul Graham's On Lisp. So I've been wondering about Python vs. Lisp lately, esp. on the topic of dynamism. I wish I knew more about Lisp to delve into this further, but your first para caught my eye.
Is it not the case that in Lisp people tend to use values — lists of (probably immutable) data — rather than objects? Objects are in and of themselves their own flavor of custom, specialized language for interacting with a data structure. In Lisp, the language for interacting with lists of lists or what have you is, well, Lisp.
IME duck typing is not a real answer in this situation; that helps when you are programming to an interface in the standard library, or in an API you are using. Otherwise I end up hunting down information about a data type through N layers of method and function calls. Not a good time. :( This actually drove me towards Go as a language with comparable productivity but with a type checker, et al.
I'm not sure where Lisp macros fit into this, if they do. I expect they have a tendency to boil down frequently repeated patterns into something very compact and less error-prone.
As for IDE support, Rob Pike suggested (not in so many words) that if you need an IDE for your programming language, it suggests that something's probably wrong with that language. He's not disputing that people genuinely need them, because yes, jesus h, I really do need Eclipse when you're programming against a reasonably large API. I think I find this line of reasoning more convincing than I would have, say, a year or two ago.
Anyway, for my part, ST2 was enough of a Python IDE for me. And thanks for the tip about Pyflakes, by the way. There's guard[1], which I figured you could rig up with python -c or whatever the flag is to compile. Pyflakes looks easier.
I would say that idiomatic python code often does not overuse objects. Lack of immutable data is indeed something I miss (especially COW dict/list/set).
One thing that is clearly overused in python code is dictionaries to pass data where the set of keys is fixed and known, or worse passing dict with different set of keys instead of proper 'modelling'. Things like traits (http://code.enthought.com/projects/traits - disclaimer, I work for Enthought ), or even simple libraries like remoteobjects (https://github.com/saymedia/remoteobjects) can be helpful in those cases.
I myself don't use IDE much: I am too entrenched with vim to gain enough from an IDE, although pycharm is quite nice. I would much rather see libraries around things IDE do ('live' 'AST' of a whole project, good refactoring, debugging) so that one can integrate them in whatever environment people use. I think we are going there (https://groups.google.com/forum/?fromgroups#!forum/python-st...).
Go does look nice, but it misses two killer features for me: NumPy and the whole scipy ecosystem, and C integration (in both directions). I think the python community itself does not realize how big and significant the scipy ecosystem is (nothing comes anywhere near in any common language AFAIK).
In a sense, it allows for poor programmers to do more.
Exactly. Dynamic typing can provide a temporary fig leaf for bad design in ways where static type checking would be less forgiving. Doesn't make dynamic typing wrong, it just means that bad programmers can get in over their heads more completely and more quickly.
Python has been my favorite language for many years, while the last few months at work I've been writing a lot of JavaScript. I've found that JS has exactly one advantage over Python, which is usable function literals. And that turns out to have a surprisingly large impact. It's convenient and natural to create "anonymous classes" that use closures to store their state, while in Python you'd have to manually create classes and manually copy the variables in __init__. I really wish Python could gain something like that while retaining its many advantages.
What do you mean by "usable function literals"? Because Python has them, even if you have to do a bit of work to get them.
def demo (n):
def multiplier (m):
return n * m
return multiplier
x5 = demo(5);
print x5(4)
You can do anything in the inner closure that you might want. The only surprise is scoping - if you want to modify state you need to use a mutable data type because otherwise you'll be instantiating a private variable. But that hoop is easy to handle.
But, you say, you really want anonymous classes? Well look up metaprogramming then. Python really has quite good support for it. There is no trouble creating anonymous classes on the fly with their own attributes, functions, and so on. They can even inherit from each other. http://www.voidspace.org.uk/python/articles/metaclasses.shtm... might be a good starting place on this for you.
(Note, I do not recommend metaprogramming unless the solution you make will be reused a lot.)
lambdas. Your example isn't a literal, it's just a declared function named "multiplier". Sure, it has equivalent functionality, in the same sense that Python could remove collection literals like [1,2,3] and you could still create lists and maps by hand. But that would be silly.
even if you have to do a bit of work to get them
And that work means that useful techniques become too unwieldy to use in practice.
I am also annoyed by the omission of a truly useful lambda. But the rest of the language is compact enough that I've never found techniques relying on closures to be unwieldy in Python. shrug
My similar solution is here https://gist.github.com/3678958 it has the added bonus of using an object with the methods attached, but the outcome is the same.
I've been heavily involved with python and javascript for most of my programming life and find them to be very similar. It's a joy to write in either language.
That's fair. My editor marks those for me automatically, so they're much less of an issue for me, but they are still really stupid.
On the other hand, you can work around that particularly stupidity just by being careful. Working around the Python limitations requires either something hacky like using a one-element list instead of a variable or (only in 3, I think) a rather ugly "nonlocal" statement at the top of your scope.
This thread mostly contains superficial complaints (whitespace, self, the GIL) that are either explicit design decisions or not important for the kind of programming Python wants to address. Some more important problems that materially affect normal and sane use of Python:
- Broken scoping by default (the thread does raise this)
- Unicode is very painful in python2 (addressed very well by python3)
- Duck typing sometimes prevents early detection of genuine programming mistakes because of overuse: False + 1 == True
- No good isolation tools (virtualenv, the most common tool, silently fails to isolate dependencies when moved, which is obviously makes it tough to deploy using it)
- Poor packaging tools (only a few tools than they all have serious limitations such as not being able to package c-extensions).
- Some of the standard library is downright terrible. csv and robotsparser are good examples. This has the pernicious effect of widely-distributing libraries that have subtle but serious drawbacks.
But being not relocatable is not really an issue, as you can simply re-run virtualenv in the new place. All libs you previously installed will work without having to install them again
> Some of the standard library is downright terrible. csv and robotsparser are good examples. This has the side effect of widely-distributing libraries that have subtle but serious drawbacks.
Some stuff in the stdlib are not good that's true, but we have a gigantic library ecosystem at pypi.python.org will very well written piece of libs. Using Python seriously, I have never ever felt the serious drawbacks you are talking about except with tarlib in python 2.4 which was seriously broken.
But c-extensions aren't put into the same package so you still have to include the shared object files yourself so it's not really a package. One of the best things about the JVM's jar is that you can pack all your other jars into one jar for deployment.
> If you mean it's not relocatable, there's a new option for this: (--relocatable)
"In theory virtualenvs have a --relocatable flag but that one is heavily broken and conceptionally can't work properly because it uses the system Python interpreter to switch to the intended environment."
If you learn to like Python, you will learn to hate other languages. :)
The stdlib. It has some great parts and overall is just good enough (and desire to not break everyone's code) to not cause revolt and wholesale replacement.
FTPlib is atrocious.
httplib/urlib mess -> requests
os os.path, shutil subprocess is a ridiculous mish-mash of legacy, obscurity and surprising behavior. One example. os.mkdir (but only the leaf, and exception if it exists), want mkdir -p behavior. Is that an option to os.mkdir, fuck no! We need a whole new function for such dramatically different feature. Must be called mkdirs, fuck no! That would be too easy to remember and no one every look at our beautiful docs. os.makedirs is the obvious choice.
My biggest issue at this point is the implicit scoping. I've been using Python for 8 years (as well as a bunch of other languages, both for work and personally), and — while a lost cause, it won't be changed at this point — I've come to believe it was a Really Bad Idea[0].
Not only does it make bindings awkward (and sometimes broken, in different ways depending on the way "scope inference" is implemented) and tends to hide errors (e.g. typo) or generate them much later than possible (even though lexical checking is very cheap to perform at parse time, with explicit scopes), it has pretty much no actual benefit I can think of.
Especially in Python where assignment is a statement, not an expression, so it's not like explicit scoping would prevent putting assignments in place they're currently allowed.
[0] and it's one of the things I find utterly retarded in coffeescript.
"If you learn to like Python, you will learn to hate other languages."
I think it would be more accurate to say that if you learn other languages, you'll learn not to hate Python. Python is kind of mediocre in a lot of ways, but it's the only language without any deal breakers.
For some people the JVM is a deal breaker. I can't say it's a deal breaker for me, but I do understand some of that sentiment.
You can't write command line tools in any JVM language because of that long startup and warmup period, and you're always going to suffer from excessive memory consumption.
Also, lack of easy access to C/C++ code is problematic for some types of software.
I agree it's not wonderful for command-line tools. We're writing a fairly large app with it (everything behind https://circleci.com). Instead of single-run command line tools, you write functions that you call from the REPL. Took me a while to get used to it, but it's pretty great once you switch over.
If you’re working on embedded code, the overhead for something like the JVM could easily be prohibitive because of the size.
And if you’re working on anything that needs to do a small job quickly, such as command line tools as fauigerzigerk mentioned, the overhead for something like the JVM will almost certainly be prohibitive because of the delay at start-up.
You can use it to make websites, do scientific computing, NLP, email processing, datamining, big data, etc. If you're just trying to throw up a website then Ruby or Javascript are fine, but if your backend involves lots of different types of computing then Python is pretty much your only option. So if you have an idea for a minimum viable product but you don't really know where you want to take it two or three years down the road, then it really just seems like the most pragmatic option.
True, but it has to be said that for NLP, scientific computing and datamining, Python is used as a job control or glue language. The actual algorithms can't be written in Python and that is Python's biggest drawback for me.
I can't tell if the last bit is being sarcastic on bad naming, or it's an oversight: the function you want is called os.makedirs http://docs.python.org/library/os.html#os.makedirs and has been around since 1.5.2.
Actually I'd preferred something object oriented with option to ignore existing and make all intermediate parts Path.make(force=False)
2nd choice: makedir(force=False)
3rd choice: makedir() and makedirs()
Never ever would I want/expect two related functions one abbreviated one not. Yes, I know history of why it's called mkdir. Should learn from history, not repeat it.
The scripting languages are all really more alike than different.
The main downside of Python compared to, Ruby, for example, is that Guido made some serious mistakes in the initial design and in the process of fixing them added a lot of extra complexity to the language. Python doesn't cleanly "fit in the head" the way Ruby or JavaScript do, at least for me.
But instead of wasting time micro-optimizing in such a narrow language space, pick Python or Ruby or whatever and spend your extra cycles on something different enough to be worthwhile like Go or Haskell.
> Python doesn't cleanly "fit in the head" the way Ruby or JavaScript do, at least for me.
I find the severe breakages and insanities of Javascript (such as tracking what `this` is) take much more brain-space than Python ever did, personally.
That "this" business is absolutely ridiculous. The fact that Javascript remains the SOLE language to script browsers in 2012 really really pisses me off. It is not a very good language in terms of design (even though it's been proved to be quite efficient and capable in the past few years).
I learned python first, and I have the same complaint about ruby. When I was attempting to learn ruby, the subtle differences between blocks, procs, and proc.news, or whatever they are, seemed arbitrary and required too much thought to understand for my taste. In my brief exposure to ruby, that was symptomatic of a general trend in the language that there seemed to be many ways to accomplish a task, without a clear reason for the distinction. That's fine if you're writing code, but imo that's a feature that makes reading a language more difficult.
You don't have to learn every single way to achieve a task though. Just sticking to simple blocks will solve 95% of the issues for you. If you want to get into more detail, Ruby will grow with you. I've sincerely yet to explicitly use lambdas or Procs and I've been coding Ruby through Rails for a few years now.
Like I said, that's fine if you’re only writing code. It doesn't stand up so well if you need to read code also. The author of the code could use any feature the language provides.
Did you learn python after you learned ruby or javascript? I have the exact same complaint about ruby not fitting in my head, and I think it's because I learned python first.
I think part of the problem comes from them being so close syntactically and semantically, it puts the second one you learn in the uncanny valley, just close enough that your brain tries to reuse previous knowledge, just far enough that it can't and you get frustrated. I think that's especially the case if you don't have many languages under your belt.
When going from Python to Haskell, or Ruby to Erlang, you can't get confused between the two.
This is true, I've tried to write Python code in CoffeeScript, for instance using Python's range() or the colon at the end of the beginning of a block.
I endorse Go as a substitute for Python. Seriously! It may read like a spiritual successor to C, but in practice, with type inference, GC, and struct/map/slice literals, it's very (as the golang people put it) light on the page.
I think "scripting language" is not the fairest identification. But not for the reasons most people don't like it: quite the opposite! For me, the issue is not that you can use Python or JavaScript for general-purpose development--although, of course, you can--but that you can actually use Haskell for scripting. And it's actually very good at it, too!
I've been replacing my shell scripts with Haskell scrips lately and the transition is going well. Soon the only two languages I will ever need are Haskell and elisp!
> Python doesn't cleanly "fit in the head" the way Ruby or JavaScript do, at least for me.
I imagine the order on which you learn is important. I learned OOP with Smalltalk and C++ not only didn't fit in my head, it created an impressive allergic reaction. It took me 8 years to be able to get back to it and finally learn.
And it still feels a horrible kludge.
I learned Python before I had contact with Ruby and I find the irregular syntax hard to parse.
I also think the python 3 transition came at a bad time. There are a lot more strong alternatives to python now than there were when the transition was planned so putting up with the hassles of migration is enough for some people to consider how green the grass might be in another field.
Guido always said he could only afford to break python once but it's not clear to me he'll even get away with once.
"The scripting languages" is a very broad category. I think Python is rather different from Tcl, AppleScript, csh, JCL, MATLAB, and VBScript, to name but a few well-known scripting language. Ruby is definitely closer to Smalltalk (a non-scripting language) than it is to PHP (a server-side scripting language).
So I don't understand your comment about it being a narrow language space.
In retrospective, I feel the python as a language was a good choice, it helped us move very fast and do a lot of things :
- One good pythonner is very productive
- It was easier to get someone aboard, it is easy to get python and to reasonnably efficient with it.
- there are libraries for everything.
- going to cython or c++ was always an easy alternative for those crtical part of the code.
The only cases where python can be problematic are :
- high reliability code like trains or planes software, because python code is not provable.
- very high performance, where the slowness of python in the not critical part of code can be a problem.
Besides, It doesn't protect you against the classic problems of a big codebase built by a different of people over a period of time. Tools like pynocle or pylint were good for this.
So, in the end, python has obvious weakness. Most of the time It seems like a solid choice (among others). Right tools for the job, etc.
I really like it's OO, it's regular and explicit. _ conventions are flexible for rapid development yet interfaces can still be hardened if required. You can do all sorts of stuff with decorators and Meta classes that you might do in macros in other languages but these are much more readable and predictable. The package system is simple and effective.
> When I want OOP, I play with Smalltalk. Or Self.
I'm reasonably sure there was an implicit "and be productive" in there. Smalltalk and Self are neat, but they don't do well on the get shit done test.
(Yes, I know it is possible to get shit done in those. Compare the difficulty of getting-shit-done in them to an environment like Java or C#, and an unbiased source is going to have to admit they fail pretty hard if only because of stuff like Maven or, to a lesser extent, NuGet.)
> I'm reasonably sure there was an implicit "and be productive" in there.
Right, let's move the goalposts and reduce the target, was there also the implicit assumption that the language should have a C-style syntax, be statically typed, run on a JITted runtime and be maintained by a big company?
...or we can assume that the guy isn't an idiot and has selected C# as an object-oriented language for getting shit done. The only one who said "play" was you, and it makes sense because there's not really a lot of getting-shit-done using Smalltalk on modern systems. But he didn't say "play", he said "use", and a reasonable person might think that he said "use" because he has shit to get done.
If hopping up and down and yelling about your pet likes when they don't really fit the topic is your bag, more power to you, but...
Lower syntactic and semantic overhead, pretty much all of the language semantics are built from two properties/behaviors (and hooks into the interpreter): blocks and sending messages.
Sending messages are method calls, the difference is mostly contextual. Blocks are first-class functions with closures and nonlocal returns. That part's important when you're trying to implement e.g. loops (otherwise you have to add flags or throw exceptions, as in Javascript).
In Smalltalk, iteration is a message and a block:
aCollection do: [item|
"process item"
]
but that's not exactly rare these days. On the other hand, a conditional is a message and two blocks:
aBoolean ifTrue: [ "code if true" ] ifFalse: [ "code if false" ]
A repeat loop is a message to a block on a block:
[ "dynamic condition" ] whileTrue: [ "repeat while true" ]
and so's a try/finally:
[ "a piece of code which may blow up" ] ensure: [
"error recovery code" ]
or a try/catch:
[ "a piece of code which may blow up]
on: SomeError
do: [ :sig| "error handling" ]
What this means is that there are very few special cases in the language, and the user of the language has the same power of expression as the designer of the language (much as in Lisps or Forths), the only limit is the hooks into the runtime but that's about it.
Also, message cascading is a minor but way cool (and pretty much unique) feature of Smalltalk, it lets you have your cake (return interesting values from methods) and eat it too (chain calls):
Another response from a different source. Having to include self as a method argument means that those are not methods, but static functions. This really means there isn't precisely methods at all and the whole OOP part of python appears hacked together. That part seriously turned me off when I encountered it.
If there is a valid reason for this, it would go a long way in explaining away one of the major reasons I dislike python.
Edit:
Appears to be a bug and I cannot respond to your post 'masklinn'. Can you rework the example to include the result as 42 being an instance variable instead? Thanks.
It's not that the language is hiding self/this so much as what manually passing those as variables represent. What you are saying is that you never need to call a method like this?
Although you can also (and that can be rather neat when e.g. mapping or filtering) use the method as a function, if you get it from the class:
f = Foo()
Foo.bar(f)
note that it will typecheck the first argument to ensure it's an instance of `Foo`, it's not a function, it's an unbound method:
>>> class Bar(object): pass
...
>>> b = Bar()
>>> b.n = 66
>>> Foo.bar(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method bar() must be called with Foo instance as first argument (got Bar instance instead)
Oh just, you know, create a function... There's nothing more pointless than @staticmethod, unless it was used specifically to port code it should mark whoever used it for a whipping.
The code to call super is awkward. Instead of saying super(MyClass, self).foo(), I would rather just say super.foo().
Having to specify self or cls as the first argument of an instance or class method. This one is weird when you are learning Python because you don't pass in the first variable but it gets magically inserted for you.
Creating a class method using a method decorator. Using a decorator to do something like this seems like a hack to me.
Not having a clean way to specify what all the instance variables are outside of the constructor. People with a java background are used to putting instance variable declarations as the first thing inside a class definition. In Python, these will be static variables. This seems weird and inconsistent with the how method declarations work (methods only become class methods when a decorator is used).
> The code to call super is awkward. Instead of saying super(MyClass, self).foo(), I would rather just say super.foo().
That's been changed in Python 3. Although it brings other issues.
> Creating a class method using a method decorator. Using a decorator to do something like this seems like a hack to me.
Why? Even more so, why is a decorator more of a hack than adding a new keyword (java) or special-casing method declaration (ruby) itself? Note that you can also create class methods without decorators if you wish to, by using a metaclass (I believe the method won't be accessible from the instance though)
> This seems weird and inconsistent with the how method declarations work.
It's actually very coherent: every binding created in the class scope is collected and set on the class object, as if passed as the third argument to `type`. That's it. Some objects (functions) are processed a bit more, but they still end up in the same place.
> methods only become class methods when a decorator is used
That's because the decorator is basically a flag to tell the attribute-resolution process to handle this method differently from the normal method-resolution process.
Class methods needing decorators really bugs me too. The problem goes back to explicit self. The explicit self, as currently implemented, carries no information: yes, you get to pick "self" as the instance name, but everyone picks "self" for that and There's One Way To Do It in Python anyway. If you could declare a class method like this:
class Foo(object):
def class_method(Foo):
pass
then suddenly explicit self gains a meaning (Hey! Instance method!) and class method declaration doesn't go through a completely unrelated mechanism.
>> methods only become class methods when a decorator is used
> That's because the decorator is basically a flag to tell the attribute-resolution process to handle this method differently from the normal method-resolution process.
Do you see how that's just special-casing method declaration in a similar way to Ruby's mechanism, just relying on the programmer to drive the mechanism himself? To a not-so-casual observer it looks like Guido wasn't willing or able to change enough semantics to make a syntactic fix work for class method declaration, despite there being an obvious space in the syntax for it to fit in, so lent on a convenient implementation detail - a hack - to get the same effect with more work for the programmer.
Of course, it's equally possible that he believes that class methods are a code smell and should be explicitly made ugly, which I could have a certain sympathy for, but to me the current situation just looks really inelegant.
> yes, you get to pick "self" as the instance name, but everyone picks "self" for that
No, actually, there have been situations in the past where using `self` was not the best choice, and I didn't. Using `self` is extremely confusing when defining methods on metaclasses for instance (self turns out to be a class object)
> If you could declare a class method like this:
You completely change the semantics of defining methods and potentially break explicit code.
> Do you see how that's just special-casing method declaration in a similar way to Ruby's mechanism, just relying on the programmer to drive the mechanism himself?
The programmer is always the driver, the difference is that decorators allow doing this without adding a pointless syntactic special case.
> No, actually, there have been situations in the past where using `self` was not the best choice, and I didn't. Using `self` is extremely confusing when defining methods on metaclasses for instance (self turns out to be a class object)
Here's somewhere that Python could have made things easier for you. What's wrong with `self` being a class object? It's something Ruby gets right, in my opinion.
> You completely change the semantics of defining methods and potentially break explicit code.
Yep. In a good way. Guido had a chance to fix this at Python 3, and didn't. Do you seriously not see the benefits of the code I posted?
> The programmer is always the driver, the difference is that decorators allow doing this without adding a pointless syntactic special case.
Yes, the programmer's always the driver, but my point is that in Python the programmer has more driving to do than necessary. The reason this is confusing and inelegant is precisely because it's not a pointless case. Decorators allow doing it, but they're absolutely not the best way, its a (ab)use of a conceptually unrelated mechanic.
^ this ^
never understood why certain methods (or are they functions?) are not called on the objects. This is one of the reasons I love Ruby, everything is clearly an object.
> ^ this ^ never understood why certain methods (or are they functions?) are not called on the objects.
Because they don't depend on a given class, they're protocols. There's no "master class" in which to put them, so they go in some sort of bastardized CLOS-like generic method instead.
With duck typing, that shouldn't matter. Lots of methods in the Python standard library take "file-like objects" that have read() methods and no common superclass, and that works fine without read being a top-level function.
It seems strange at first but now it feels pretty natural. The alternatives might be to have a new global function (like len) or to have every possible iterator type have join (which sounds complicated).
Not the one you replied to, but one thing that hits me as awkward is the default static behavior of variables.
something like
class Foo:
bar = 1
means that bar is now a static class member (static in Java's sense). It needs to be assigned to "1" explicitly in __init__ as far as I understand. That's just unintuitive and leads to lots of boilerplate, just so that you don't need specifiers like 'static'. Sometimes the fear of verbosity gets in the way of intuitiveness.
> Not the one you replied to, but one thing that hits me as awkward is the default static behavior of variables.
There's no default anything, what you wrote sets "bar = 1" on the class itself, nothing more an nothing less, just as if you'd written:
Foo = type("Foo", (object,), {'bar': 1})
(in fact that's pretty much what the class statement is sugar for)
If you want to set it on the instance, set it on the instance.
Python's class context is a namespace like every other, you can do any computation you wish in there, and at the end the ``class`` statements collects all bindings of the namespace and sets them on the class.
Well in my case foo = Foo(); print foo.bar will print 1, so it does 'kinda' work as a default value. And that's ok, I just remembered there to be some case where it didn't behave the way I expected it to (e.g. instance member assignment overriding the content of another instance), but I wasn't able to reproduce it right now.
> Well in my case foo = Foo(); print foo.bar will print 1
Of course it will, Python's attribute-resolution (as do most languages) tries the instance, then the class, then the superclass, then... why wouldn't it work?
> I just remembered there to be some case where it didn't behave the way I expected it to (e.g. instance member assignment overriding the content of another instance)
Only two way I see for that to happen:
1. Re-setting the member on the class itself, not the instance
2. Or having the member be mutable and mutating it, since it's set on the class it's shared by all instances.
Right, of course it was the mutable use of an internal data structure.
class Foo:
bar = [1]
foo1 = Foo()
foo2 = Foo()
foo2.bar.append(2)
print foo1.bar
print foo2.bar
prints
[1, 2]
[1, 2]
And that's the reason why I don't like that the standard way I define common instance members is an init function. Are there any decorators I could use to do that outside of any class function?
> It needs to be assigned to "1" explicitly in __init__ as far as I understand.
I don't think so. You do have to assign anything to it if you want to use it as a 'static' (actually 'class' attribute). You can access bar from Foo, as in Foo.bar or from an instance of Foo as in foo=Foo(); foo.bar.
What probably is confusing to you is if you assign a value bar in an instance then that gets assigned to the instance not the class. That is if you do foo.bar=100 then now foo instance has an object attribute called bar=100 that overrides Foo.bar=1. But Foo.bar still stays at 1.
What's even worse IMO is the lack of a proper built-in 'super'. The self parameters at least serve the purpose of deciding whether a function is a class function or object function.
So yeah, I agree, python's OOP is kind of awkward. I love the ease of composition though, something I use extensively even in other languages since I know python.
"It's far from the metal"? No it isn't. It has ctypes, which is one of the best FFIs I've worked with. I wrote a trampoline-injecting kernel debugger in it. I don't love Python, but that answer disqualifies itself.
You're probably aware of LuaJIT (http://http://luajit.org/) already but I'll toss it in for other readers: as a former heavy user of ctypes, after discovering LuaJIT and its FFI I haven't touched ctypes again. For people who need to do this kind of work, it's worth looking into (enough so that one of the core PyPy developers is emulating LuaJIT's FFI design in the project Python CFFI http://cffi.readthedocs.org/en/latest/index.html).
Instead of implementing breakpoints with INT 3, which relies on the OS's debug event support, our debugger injected new basic blocks into the program to pause the target, perform I/O with it, and redirect it to other code paths.
You should have no trouble looking up what a "trampoline" is, but I'm abusing the term a bit.
So you made a debugger below the OS by instrumenting the target program to do debuggery things? That's pretty cool. But at what granularity did you inject the code? Presumably not around every single instruction. But if it was just on demand wherever the user set a breakpoint, how would you implement stepping?
We didn't do stepping (at least, mine didn't); instead, we tended to do things like Detours, where we'd target a broad set of bblocks in the program and either log, or snapshot the process when they were hit.
There is a pattern for single-stepping using similar techniques: "user mode single stepping".
One thing to bear in mind: it was easy for us to do this in part because crashing the target program was not a big deal for us.
My biggest problem with Python is not that it's a bad language but that it isn't a great language. Why is this an issue? Simple: it's one of the most highly rated--and, frankly, overrated languages today. And, try as I may, I can never seem to get away from it: at least in my circles, it's even more endemic than Java!
I really wish people liked OCaml or Haskell that much instead.
Yeah. Python has an awful lot of warts, considering the way so many people fawn over it.
Really the main strength of Python is not the language at all, but the huge number of libraries written for it, and the wealth of tools and "community." Fair enough, it was in the right place, at the right time, but if history is going to throw its weight behind something, it is a little annoying when that thing is rather mediocre...
It's an interesting read - he begins with "The problem is that it is not as good as it is made out to be, in other words it suffers from a degree of hype. I'll try to argue this point."
Mostly it is the age old complaint, can't build something big in a dynamic language. The evidence is that I don't know of anything big in a dynamic language. Not exactly great points against python when the argument boils down to, "all dynamic languages are bad."
- usually constants are capitalized, but math.pi and math.e aren't
- the Decimal class has methods like is_nan(), and generally puts underscores in the method names, while the math package provides methods like isnan()
- even within the same package, you have cases like itertools.groupby() and itertools.combinations_with_replacement()
Not a deal breaker, it's just a drag when you only work in a language once every few months, and you constantly have to look at the docs to know whether something is alllowercase, ALLCAPS, camelCased, with_underscores, etc.
> - Debugging is a little behind other languages (pdb works)
So do the numerous extensions/replacements for pdb, or the multiple graphical debuggers (e.g. PyCharm's, PyDev's, PuDB, WinPDB, Kodomo IDE's, Wing's, ...). Python doesn't have a time-traveling debugger à la OCaml, but that's about it as far as I can tell.
> removal of map/filter in place of list comprehensions (they are certainly better but not faster)
What are you talking about exactly? Because I find two possible interpretations of that phrase, and neither comes close to being correct.
> More frustrating to me is that there isn't an imap/ifilter builtin
FWIW, alongside the demotion of reduce to the stdlib Python 3 changed that, the builtin `map` and `filter` now return lazy "views" (as do e.g. `dict.items`, `dict.keys` and `dict.values`):
Speed, definitely. Although in defence Ive always found python very easy to "hack".
Writing a C extension to do computationally intensive work is fairly simple.
With its versatility and extensability I once threw together a proof of concept password cracking cluster in a week. Redis for queues/data, MySQL for data, hashing code in a C extension, flask for the web interface. Cross platform and lovely :-)
There's so many solutions to the "speed" argument I don't even see it as an issue.
- C-extentions (many std and 3rd party libs that need speed are written in C)
- numpy http://numpy.scipy.org/
- numba https://github.com/numba/numba
- cython http://www.cython.org/
- pyrex http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/
- http://psyco.sourceforge.net/ -> PyPy (some day...)
Where you were already using external queues, threads shouldn't have been an issue: just have multiple processes going (one for each core). Or use the processing package if you want more "threadlike" control.
It is unfortunate that you have to know that kind of thing a priori.
"Oh, look, a nice, built-in threading module."
Yes, BUT...
EDIT: I just re-read the way you wrote the "threading" comment and realized you probably weren't talking about it in the same context. Amazing what a word or two can do to understanding.
That thread is depressing - most of the things people are complaining about are conscious design decisions made by the python team. EG Complaining about self - explicit is better than implicit is a core property of python. I feel like `import this` would answer a lot of these.
Practicality beats purity. Python should support better function literals for the same reason that it supports the "implicit" [1,2,3] instead of making you explicitly create a list and call append. I'd say self falls into the same category, although I haven't found it as much an annoyance as some.
I would say that Python has a feeling of inconsistency at times compared to other scripting languages. I don't mean this as a criticism. I like and use Python a lot for work and personal projects. Here's an example of what I mean by inconsistent:
len(a_string)
a_string.lower()
a_string.upper()
So here, there's a global, generic function len() that returns the length of the string while in the other examples, the string has methods such as .lower() .upper() etc. Why not have a_string.length()? I understand that generic functions are more efficient and even proper in OO. It just causes the feeling of inconsistency when you use the language a lot or when you are teaching a child how to program and have to explain the difference and why some things are methods while others are not.
Also, some python idioms (such as slicing) are not intuitive for many use cases. Here's an example of the traditional way in which to reverse a string in Python by slicing:
a_string[::-1]
In Ruby, you type a_string.reverse() and that is much more intuitive to a child when compared to the slicing in Python.
Having said all of that, I love Python and use it daily and encourage others to do so as well.
I
My least favourite part of python (I'm overwhelmingly a fan, and it's probably my favourite language after Clojure) is the deliberately limited functional parts. Take list comprehensions, they work for simple things, but when you want to make it more complex, it wont let you break it over multiple lines, so you have to rewrite it into a for loop. Makes debugging awkward.
result = [
(value, process(value), process(key, value))
for key in generator
for value in generate(key)
if some_condition(key)
if other_condition(value)
]
or did you mean something completely different? Maybe the problem of not being able to use statements in comprehensions (or lambdas)?
The GIL does not make threads less evil than you apparently already find them, it just makes them a lot less useful.
Threads in Python still have all the pitfalls and complications they have in other languages, it's just that they cannot use multiple cores or hardware threads. So you get all the evil bits (race conditions, deadlocks, having to protect access to shared state, etc), but you miss out on all the performance benefits threads can have on parallel hardware.
In Python, threads are only useful as an abstraction of things that have to run concurrently, even though they never do.
Not that I want to distract of your righteous rant, but threads also work without issues when doing mostly IO: C libraries and implementations are free to release the GIL (and often do). So if you want to e.g. fetch a bunch of things concurrently, those will be done concurrently. And your
> have to run concurrently, even though they never do.
is way off base.
Likewise, if you're doing CPU-heavy crunching (likely implemented in C or Cython), you can release the GIL and let the interpreter run some other bytecode at the same time.
> In Python, threads are only useful as an abstraction of things that have to run concurrently, even though they never do.
This is just false. Threads still offer performance benefits for non-CPU bound operations. For example, if you are trying to download data from 100 different sources, you could do it in a single thread, but python would spend most of its time idling while other prosseses do work, or you could have 100 threads, so python can do work while it is waiting on other stuff to get done.
I was a Lisp/Scheme programmer. I use Python only because Norvig wrote that it isn't so bad. But I curse out loud every time I type in 'self'. I hate the lambda limitation (only expr). The scoping rules continue to confuse me. It's a good language if you're coming from C/Java. But it's a poor language if you're coming from Lisp/Smalltalk/ML.
Fundamentally, I believe Python's drawback is that it appears to draw heavily from the ABC history it has, and wasn't designed for power and flexibility.
Nice to see programmers.stackexchange.com being used for things like this. Hopefully it will put a rest to the same top comment on HN Stackoverflow stories complaining about how such questions are booted off Stackoverflow.com
Ironically, the one time where I noticed this bug was while testing a component, whose only use involved me emulating the behavior of mutable default arguments on a higher level. I still think it is a horrible system.
My strong opinion on it might be because I spent hours looking at our c-extensions for the source, because the previous two 'language bugs' I ran into were both in our c-extensions, where we had a type that could not cast to it self, and someone thought it was a good idea to make ~x mean x-1.
The speed issue is a significant one, but not usually for the reasons given. The big drawback of being slow means a lots of its implementation is not written in python.
IDE/debugger is another issue, although some of it is a matter of people not knowing the tool. Especially on bad codebases, even something as simple as pyflakes running in the background is invaluable, and many people don't use this.