def index(self):

Duncan Booth duncan.booth at invalid.invalid
Thu Dec 21 04:44:48 EST 2006


"George Sakkis" <george.sakkis at gmail.com> wrote:

> Gert Cuykens wrote:
>> > > class HelloWorld(object):
>> > >     @cherrypy.exposed
>> > >     def index(self):
>> > >        return "Hello World"
>>
>> do i have to write @cherrypy.exposed before every def or just once
>> for all the def's ? and why not write something like @index.exposed ?
>>
>> in other words i have no idea what @ actually does i only know i have
>> too write it to make it work :) Am guessing @ is something like
>> prototyping in javascript but then def index is not a object but a
>> method ? So that would not make sense ?
>>
>> oh and self stands more or less for private method right ?
> 
> Ouch. Do you really expect to learn a language from scratch by Q&A in
> a malining list ? That's a very inefficient way to spend both yours
> and others' time. Do your homework first by reading one of the several
> free tutorials online and come back when you have trouble with
> something specific you didn't understand.
> 
To be perfectly fair, searching for information about decorators if you
don't know what they are called is kind of hard. Searching for
information about the '@' character in the Python documentation doesn't
lead to much. The CherryPy tutorial does at least mention this
decorator, just not in any meaningful way: 

> Exposing objects
> 
> CherryPy maps URL requests to objects and calls the suitable method
> automatically. The methods that can be called as a result of external
> requests are said to be exposed. 
> 
> Objects are exposed in CherryPy by setting the exposed attribute. This
> an be done directly on the object itself: object.exposed = True.
> Methods can also be exposed using a special decorator:
>
> Why this distinction? 
> 
> The distinction between published and exposed objects may seem silly
> or unnecessary at first. By definition, all exposed objects are also
> published, so why do we need to care about non-exposed objects?
 
To the OP: 


> do i have to write @cherrypy.exposed before every def or just once for
> all the def's ?

Before every def that you wish to expose, which won't be all of them.

> and why not write something like @index.exposed ?

because that isn't how it works. cherrypy.exposed is a decorator, which 
means it is a function that takes a function as an argument modifies it in 
some way and returns the modified function as its result.

> 
> in other words i have no idea what @ actually does i only know i have
> too write it to make it work :) Am guessing @ is something like
> prototyping in javascript but then def index is not a object but a
> method ? So that would not make sense ?

@expr
def fn(...): ...

is exactly equivalent to:

def fn(...): ...
fn = (expr)(fn)

except that at the point when the decorator is called the function has not 
yet been assigned to the name 'fn'. (Also remember that def is an 
executable statement so it happens when the line containing the def is 
execfuted, and that names assigned to functions are just like other 
variable names and can be rebound at will.)

> 
> oh and self stands more or less for private method right ?

if you have to ask that question about 'self' you really do need to read 
some introductory texts on Python.

Methods get passed their instance as their first parameter (usually this is 
implicit in the call, but it is always explicitly shown in the method) and 
self is simply the conventional name. Many other languages use 'this' 
instead of self and make the passing of 'this' implicit inside the method 
as well as outside. There are good reasons why it is explicit in Python, 
but just remember that the first parameter a method receives is always the 
instance and you won't go far wrong.



More information about the Python-list mailing list