W
Wildemar Wildenburger
Hehe, devious idea. I might just try that.TheFlyingDutchman said:Maybe you can sneak Jython into the mix. Just describe it as "this
Java scripting language".
/W
Hehe, devious idea. I might just try that.TheFlyingDutchman said:Maybe you can sneak Jython into the mix. Just describe it as "this
Java scripting language".
Maybe. Personally, I like it the way it is in Python.
Why don't you make a preprocessor which accepts method declarations
without "self" and fixes them?
Bruno said:Bjoern Schliessmann a écrit :
The problem being that there's no such thing as a "method
declaration" in Python
- only functions being attributes of a class...
(ok, I know, you meant "functions declared within a class
statement").
Maybe. Personally, I like it the way it is in Python.
Why don't you make a preprocessor which accepts method declarations
without "self" and fixes them?
Specifically an easier way of doing it provided by the language syntax
(hence "syntactic sugar"). As in, "the form 'foo += 1' is syntactic
sugar for 'foo = foo + 1'".
Chris Mellon said:and I'll punch a kitten before I accept having to read
Python code guessing if something is a global, a local, or part of
self like I do in C++.
Exactly: the technical objections that are being raised are bogus, and
the REAL objections from the Python community boil down to: we like it
better the way it is now. Bringing technical objections that are easily
debunked doesn't _strengthen_ our real case: in fact, it _weakens_ it.
So, I'd rather see discussants focus on how things SHOULD be, rather
than argue they must stay that way because of technical difficulties
that do not really apply.
The real advantage of making 'self' explicit is that it IS explicit, and
we like it that way, just as much as its critics detest it. Just like,
say, significant indentation, it's a deep part of Python's culture,
tradition, preferences, and mindset, and neither is going to go away (I
suspect, in fact, that, even if Guido somehow suddenly changed his mind,
these are issues on which even he couldn't impose a change at this point
without causing a fork in the community). Making up weak technical
objections (ones that ignore the possibilities of __get__ or focus on
something so "absolutely central" to everyday programming practice as
inspect.getargspec [!!!], for example;-) is just not the right way to
communicate this state of affairs.
ago, TheFlyingDutchman has forked Python and is working on a very special
new language, PIEthun 3.01B.
I for one am looking forward to seeing all
the very special features of PIEthun.
Yep, there are only definitions. I'm sorry.
What, IYHO, is the difference between a method and a function?
>>> Foo.__dict__['meth']>>> Foo.meth>>> Foo().meth
I think that those functions _are_ special ones
since the compiler
is able to make "method(instance, a, b)" out of
"instance.method(a, b)".
>>> Foo.fun = fun
>>> Foo.fun>>> Foo().fun>>> fun>>> Foo.__dict__['fun']
So IMHO, "method definition" makes sense.
Bruno said:A method is a thin wrapper around a function, usually instanciated
and returned by the __get__ method [1] of the function itself when
the function is looked up as an attribute of a class or an
instance:
[...]
They aren't, and you should perhaps read the manual - all this is
documented.
Terry said:No it does not. The method wrapping is done at runtine. The
compiler is ignorant of the wrapping that will be done.
1 0 LOAD_GLOBAL 0 (c)
3 LOAD_ATTR 1 (meth)
6 CALL_FUNCTION 0
9 RETURN_VALUE
The function gets wrapped as a bound method as part of LOAD_ATTR.
When the compiler sees <expr>(args), it does not know and does not
care about the
particular type that <expr> will become. It just assumes that it
will be
callable and emits the code to call it. Consider
Piet van Oostrum said:It is more than just syntactic sugar because the Class is derived from
the instance at runtime.
"this" in C++ and Java is not shown in the parameter list, which was
what he was
complaining about. He wants
class MyClass:
def SomeFunction(someParameter):
self.someParameter = someParameter
not
class MyClass:
def SomeFunction(self, someParameter):
self.someParameter = someParameter
The confusing way about the current Python method when you first
encounter it is
why is "self" being passed in when you write the function but not
when you call it. If the compiler is smart enough to know that
a = MyClass()
a.SomeFunction(12)
SomeFunction() has a "self" implicitly added to the parameter list, it
seems that it should be smart enough to know that a function defined
in a class has a "self" implicitly added to the parameter list.
"this" in C++ and Java is not shown in the parameter list, which was
what he was
complaining about. He wants
class MyClass:
def SomeFunction(someParameter):
self.someParameter = someParameter
not
class MyClass:
def SomeFunction(self, someParameter):
self.someParameter = someParameter
Bruno said:TheFlyingDutchman a écrit :
def fun(obj, *args, **kw):
# generic code here that do something with obj
import some_module
some_module.SomeClass.fun = fun
This is why uniformity is important.
But anyway, I think it's quite clear that Python won't drop the explicit
self, so it looks like you have to live with it or choose another language.
to methods. class methods gets the class as first parameter.
Anyway, there are a lot of things that Python doesn't do like "other
mainstream OO languages", and that's a GoodThing.
I definitively think you'd be happier with some other language.
TheFlyingDutchman said:The confusing way about the current Python method when you first
encounter it is
why is "self" being passed in when you write the function but not
when you call it. If the compiler is smart enough to know that
a = MyClass()
a.SomeFunction(12)
SomeFunction() has a "self" implicitly added to the parameter list, it
seems that it should be smart enough to know that a function defined
in a class has a "self" implicitly added to the parameter list.
Several languages use the "object.method(args)" form, which is
syntactic
sugar for "method(object, other_args)" which Ada, for instance, uses.
Knowing this clears up half the confusion (the object /is/ passed as a
parameter whichever syntax is used).
The other half of the confusion is cleared up by considering that
Python methods are ordinary functions that don't magically "know" in
which "class" context they are executing: they must be told via the
first parameter.
Pretty close. This is one of the things that's always puzzled me about
the discussion. Making self and cls keyword pseudo-constants that get
the current instance and class from the stack frame (or raise an
exception) would eliminate them from the method header.
It would ALSO eliminate a whole level of indirection in method
invocation and get rid of the instancemethod, classmethod and
staticmethod wrapper classes. This would be a significant
simplification. If it had been done earlier, it would have eliminated
most of the justification for method attributes (those silly @
things),
thus showing that unneeded complexity breeds more unneeded
complexity.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.