[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] @



Thanks for the link.







--- On Wed, 12/31/08, Lenard Lindstrom <len-l@xxxxxxxxx> wrote:

From: Lenard Lindstrom <len-l@xxxxxxxxx>
Subject: Re: [pygame] @
To: pygame-users@xxxxxxxx
Date: Wednesday, December 31, 2008, 1:18 PM

Sort of. Functions in Python are just objects. They can be passed 
around, assigned to variables, and even have attributes added to them. A 
decorator is a way to take a newly created function and do something to 
it, such as create a new function that calls it. The most common use of 
decorators is in creating static and class methods. Old way:

class C(object):
    def method(x):
        # A static method. It is just a function, no 'self'
        print x
    method = staticmethod(method)

New way:

class C(object):
    @staticmethod
    def method(x):
        print x

In both cases staticmethod is the same python type that takes a function 
as an argument and creates a static method instance which is then 
assigned the name "method". But all this is outside of the scope of this 
mailing list. All you could ever want to know about decorators is here:

http://www.python.org/dev/peps/pep-0318/

Lenard


Yanom Mobis wrote:
> Ohhhh!  I get it now! It's used to insure that a specific function is always called before another. Thanks for clearing it up for me.
> --- On Wed, 12/31/08, Michael Phipps <michael.phipps@xxxxxxxxxxx> wrote:
>
> From: Michael Phipps <michael.phipps@xxxxxxxxxxx>
> Subject: Re: [pygame] @
> To: pygame-users@xxxxxxxx
> Date: Wednesday, December 31, 2008, 4:37 PM
>
> Yanom - 
>
> A decorator is a method that takes another method as a parameter so that it can do something. It is usually used for aspect oriented programming.
>
> For example:
>
> def logThisMethodCall(methodCall)
>     # Do some logging here
>
> @logThisMethodCall
> def myMethod(a,b,c)
>     # do Somthing in here
>
> Now, whenever you call "myMethod", logThisMethodCall gets called first, with the invocation of myMethod passed into it. You can use it for logging, security (i.e. does this person have permission to be calling this), etc.
>
> Michael
>
>
>
>
> -----Original Message-----
> From: "Yanom Mobis" [yanom@xxxxxxxxxxxxxx]
> Date: 12/31/2008 11:19
> To: pygame-users@xxxxxxxx
> Subject: Re: [pygame] @
>
> so when you do this:
>
> @foo
> def bar(): pass
>
> you assume that a function foo() already exists.
>
> and it creates something like this:
>
> def foo():
>     def bar(): pass
>     pass
>
> ?
> I'm sorry, I just got confused.
>    
>
>
>
> - On Wed, 12/31/08, Noah Kantrowitz <noah@xxxxxxxxxxxxxx> wrote:
> From: Noah Kantrowitz <noah@xxxxxxxxxxxxxx>
> Subject: Re: [pygame] @
> To: pygame-users@xxxxxxxx
> Date: Wednesday, December 31, 2008, 3:01 AM
>
> decorator. The short version is that this
>
> @foo
> def bar(): pass
>
> is the same as this
>
> def bar(): pass
> bar = foo(bar)
>
> The long version is "look it up because it gets very complicated and
> voodoo-ish"
>
> --Noah
>
> On Dec 30, 2008, at 9:55 PM, Yanom Mobis wrote:
>
>   
>> I was reading some Python code examples, and i found the @ symbol. What
>>     
> exactly does this operator do?
>   
>
>
>
>
>       
>
>
>
>
>
>       
>
>   


-- 
Lenard Lindstrom
<len-l@xxxxxxxxx>