However, the syntax for the explicit self is somewhat awkward. Instead of: def f(self, a, b): ... it could have been def self.f(a, b): ...
[This isn't original to me - Guido has mentioned it when he gets tired of hearing about explicit self.]
With the second, the structure of the argument list in the definition looks like the structure of the argument list in the call and the "self" is where the instance comes from relative to the method name.
def foo(self, x):
self.bar(x, 42)
class MyClass:
def bar(self, x, y):
print ("%s %s" % (x, y))
MyClass.foo = foo
While this is ugly, there are times when augmenting (or replacing) a class's existing methods is useful.Furthermore the claim that python doesn't support objects cleanly or easily is unfounded: 1) everything is an object in python (http://diveintopython.org/getting_to_know_python/everything_...), including classes. 2) properties of an object are accessed in just the same way as in Java (obj.property), I don't understand where the confusion lies.
I regularly monkey-patch builtin standard libraries. Bug in httplib? Easy - I overwrite the built in version, or I do some other trick. It's just Python, get a class that needs a method or an attribute? Just add it.
That's the nice part of "nothing is truly private" in python - and a great argument against bondage and chains style of encapsulation/privacy. If you need to reach in and "fix" something, you can.
Encapsulation is the wrong answer to some problems. When it's the right answer (large, disperse, avg or lower ability developers aka "enterprise") go use a language built for that environment such as Java.
Same with self.
[to OA] Not all languages should be applicable to all problems. Quit trying to change Python into your language and leave it as my language.
Note the word "accidental". In a language such as Python, there is no way to completely prevent clients from accessing internal state. Even C++ or Java allow sufficiently knowledgeable / desperate clients to poke around wherever they like. Python's philosophy is that since clients will access internal state anyway, it's best to have a sane, documented mechanism.
This reads like he believes the standard library is the only library, which (considering he uses C++ and Java) is a truly bizarre belief. If he doesn't like the standard HTTP library, it's trivial to either distribute a modified version, or use somebody else's (eg, libcurl). Many large Python projects distribute variants of standard library modules to work around errors or missing features in legacy versions of Python. The problem with an implicit "self", aside from it violating the Python Zen, is that it makes a number of cases nastily ambiguous. For example, consider the following method: In this code, the only way to know the scope of 'a' and 'b' is to read the entire class definition. Using explicit scoping prevents ambiguity.Another advantage, though much less, is that it discourages excessive numbers of attributes. Somebody coming from a Java background probably likes having a few hundred attributes floating around in various states as his objects run, but I hate having to deal with code that has so many "moving parts". Adding a "tax" of five characters per attribute access/modification makes code much easier to read and debug.
There are many ways to define members in a class definition. The most heavy-handed is the "__slots__" mechanism, but you can also use properties or simple "foo = None" statements depending on the level of control clients should be allowed.If the author doubts that Python can restrict attribute modification, he should try assigning to a tuple's attributes.
Not "more like", are. Python classes and objects are (in most cases) dictionaries. This is completely wrong. I've stated this in another comment, but Python's lambdas may be of arbitrary size. Lambdas may only contain a single expression, but (as any LISP or Haskell programmer can tell you), this is not a substantial drawback. If you'd like to pass multiple statements, just define a procedure! That's why they exist!