Why can function definitions only use identifiers, and not attribute references or any other primaries?

Alan Franzoni alan.franzoni.xyzz at gmail.com
Thu Apr 23 16:53:40 CEST 2009


Jeremy Banks was kind enough to say:

> Hi. I'm sure there've been debates about this before, but I can't seem
> to figure out what to search for to pull them up, so I'm asking here.

I'm not exactly sure what you mean... if you want to add functions to
another fuction, just do it this way:

def foo():
    return "i'm the main function."

foo.bar = lambda: "i'm the bar attr"

print foo()
print foo.bar()

>     def foo.bar():
>         return("I'm a method!")

So you'd just like some "syntactic sugar" to let function declarations to
be "shorter"?

I think this wouldn't make the language that clear. In order to accomplish
what you want, you could just use a class and the __call__ method for the
"main" function.

I think that what you really mean is that you would like an "improved
lambda" or "different def". E.g., that could you write anonymous functions
( or ruby-like code blocks ) in a different way. Something like this:

d["func"] = def pippo(a, b, c):
			...
			...
			...

I suppose the reason behind this is that it's not really needed. There're
plenty of ways to do this  sort of things in python; if you just declare
methods in a class you can just use the plain def in order to achieve this,
and you can inherit from your container and specialize some methods in
order to achieve results which are similar to those you want.



-- 
Alan Franzoni <alan.franzoni.xyzz at gmail.com>
-
Remove .xyzz from my email in order to contact me.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E



More information about the Python-list mailing list