Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
generators as decorators simple issue
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
j.m.dagenh...@gmail.com  
View profile  
 More options Sep 11 2012, 10:28 pm
Newsgroups: comp.lang.python
From: j.m.dagenh...@gmail.com
Date: Tue, 11 Sep 2012 19:28:10 -0700 (PDT)
Local: Tues, Sep 11 2012 10:28 pm
Subject: generators as decorators simple issue
I'm trying to call SetName on an object to prevent me from ever having to call it explictly again on that object. Best explained by example.

def setname(cls):
    '''this is the proposed generator to call SetName on the object'''
    try:
        cls.SetName(cls.__name__)
    finally:
        yield cls

class Trial:
    '''class to demonstrate with'''
    def SetName(self, name):
        print 1, 1

@setname
class Test(Trial):
    '''i want SetName to be called by using setname as a decorator'''
    def __init__(self):

        print 'Yay! or Invalid.'

if __name__ == '__main__':
    test = Test()

How can i fix this?
This is my exact error: python decors2.py
Traceback (most recent call last):
  File "decors2.py", line 23, in <module>
    test = Test()
TypeError: 'generator' object is not callable


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ramchandra Apte  
View profile  
 More options Sep 11 2012, 10:55 pm
Newsgroups: comp.lang.python
From: Ramchandra Apte <maniandra...@gmail.com>
Date: Tue, 11 Sep 2012 19:55:24 -0700 (PDT)
Local: Tues, Sep 11 2012 10:55 pm
Subject: Re: generators as decorators simple issue
On Wednesday, 12 September 2012 07:58:10 UTC+5:30, pyjoshsys  wrote:
> I'm trying to call SetName on an object to prevent me from ever having to call it explictly again on that object. Best explained by example.

[snip]
In your decorator, you are using `yield cls` - it should be `return cls` 99.99% of the time.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
alex23  
View profile  
 More options Sep 12 2012, 12:39 am
Newsgroups: comp.lang.python
From: alex23 <wuwe...@gmail.com>
Date: Tue, 11 Sep 2012 21:39:52 -0700 (PDT)
Local: Wed, Sep 12 2012 12:39 am
Subject: Re: generators as decorators simple issue
On Sep 12, 12:28 pm, j.m.dagenh...@gmail.com wrote:

> def setname(cls):
>     '''this is the proposed generator to call SetName on the object'''
>     try:
>         cls.SetName(cls.__name__)
>     finally:
>         yield cls

A generator is (basically) a callable that acts like an iterator.
You'd use a generator if you wanted to loop with for or a list
comprehension across the output of the generator: for foo in
setname(Test)

A decorator is a callable that takes another callable as an argument,
either modifying it or returning a wrapped version of it: Test =
setname(Test)

You don't want to iterate over anything, so you should change `yield`
to `return`.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Rachel  
View profile  
 More options Sep 12 2012, 2:10 am
Newsgroups: comp.lang.python
From: Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa...@spamschutz.glglgl.de>
Date: Wed, 12 Sep 2012 08:08:02 +0200
Local: Wed, Sep 12 2012 2:08 am
Subject: Re: generators as decorators simple issue
Am 12.09.2012 04:28 schrieb j.m.dagenh...@gmail.com:

I am not sure what exactly you want to achieve, but I see 2 problems here:

1. Your setname operates on a class, but your SetName() is an instance
function.

2. I don't really understand the try...finally yield stuff. As others
already said, you probably just want to return. I don't see what a
generator would be useful for here...

def setname(cls):
      '''this is the proposed generator to call SetName on the object'''
      try:
          cls.SetName(cls.__name__)
      finally:
          return cls

and

class Trial(object):
     '''class to demonstrate with'''
     @classmethod
     def SetName(cls, name):
         print 1, 1

should solve your problems.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
pyjoshsys  
View profile  
 More options Sep 12 2012, 6:22 am
Newsgroups: comp.lang.python
From: pyjoshsys <j.m.dagenh...@gmail.com>
Date: Wed, 12 Sep 2012 03:22:31 -0700 (PDT)
Local: Wed, Sep 12 2012 6:22 am
Subject: Re: generators as decorators simple issue
The output is still not what I want. Now runtime error free, however the output is not what I desire.

def setname(cls):
    '''this is the proposed generator to call SetName on the object'''

    try:
        cls.SetName(cls.__name__)
    except Exception as e:
        print e
    finally:
        return cls

class Trial(object):
    '''class to demonstrate with'''
    def __init__(self):
        object.__init__(self)
        self.name = None

    @classmethod
    def SetName(cls, name):
        cls.name = name

@setname
class Test(Trial):
    '''i want SetName to be called by using setname as a decorator'''
    def __init__(self):
        Trial.__init__(self)

if __name__ == '__main__':
    test = Test()
    print 'instance'
    print '', test.name #should be Test
    print 'class'
    print '', Test.name

The output is: python decors2.py
instance
 None
class
 Test

I want:
instance
 Test
class
 Test

Is this possible in this manner?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Oscar Benjamin  
View profile  
 More options Sep 12 2012, 6:47 am
Newsgroups: comp.lang.python
From: Oscar Benjamin <oscar.j.benja...@gmail.com>
Date: Wed, 12 Sep 2012 11:47:02 +0100
Local: Wed, Sep 12 2012 6:47 am
Subject: Re: generators as decorators simple issue
On Wed, 12 Sep 2012 03:22:31 -0700 (PDT), pyjoshsys
<j.m.dagenh...@gmail.com> wrote:
> The output is still not what I want. Now runtime error free,

however the output is not what I desire.

> def setname(cls):
>     '''this is the proposed generator to call SetName on the

object'''

>     try:
>         cls.SetName(cls.__name__)
>     except Exception as e:
>         print e
>     finally:
>         return cls

I would write the function above in one line:

cls.name = name

> class Trial(object):
>     '''class to demonstrate with'''
>     def __init__(self):
>         object.__init__(self)
>         self.name = None

Remove the line above. The instance attribute self.name is hiding the
class attribute cls.name.

Oscar


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
pyjoshsys  
View profile  
 More options Sep 12 2012, 7:15 am
Newsgroups: comp.lang.python
From: pyjoshsys <j.m.dagenh...@gmail.com>
Date: Wed, 12 Sep 2012 04:15:10 -0700 (PDT)
Local: Wed, Sep 12 2012 7:15 am
Subject: Re: generators as decorators simple issue
so decorators only pass the object and not any instance of the object as the implied argument? Is this right?

The idea was to use @setname instead of instance.SetName(instance.__name__).

I thought decorators would do this, but it seems not.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Kelly  
View profile  
 More options Sep 12 2012, 11:10 am
Newsgroups: comp.lang.python
From: Ian Kelly <ian.g.ke...@gmail.com>
Date: Wed, 12 Sep 2012 09:09:08 -0600
Local: Wed, Sep 12 2012 11:09 am
Subject: Re: generators as decorators simple issue

On Wed, Sep 12, 2012 at 4:22 AM, pyjoshsys <j.m.dagenh...@gmail.com> wrote:
> The output is still not what I want. Now runtime error free, however the output is not what I desire.

[SNIP]

> class Trial(object):
>     '''class to demonstrate with'''
>     def __init__(self):
>         object.__init__(self)
>         self.name = None

>     @classmethod
>     def SetName(cls, name):
>         cls.name = name

[SNIP]

The SetName class method sets the name on the *class* dictionary.  The
class's __init__ method also sets a name (None) on the *instance*
dictionary.  From an instance's perspective, the instance dictionary
will shadow the class dictionary.  If you remove the attribute from
the instance dictionary entirely (delete the "self.name = None" line),
and leave the class dictionary as is, then you will get the output you
want (although from your later post I am not certain that this is the
behaviour you want).

On Wed, Sep 12, 2012 at 5:15 AM, pyjoshsys <j.m.dagenh...@gmail.com> wrote:
> so decorators only pass the object and not any instance of the object as the implied argument? Is this right?

Right.

> The idea was to use @setname instead of instance.SetName(instance.__name__).

The appropriate place to do this so that it applies to all instances
of the class rather than to the class would be inside the __init__
method.

Also, instances don't have a __name__ attribute, so it's still unclear
to me what you're looking for.  Did you mean the effect to be that of
"instance.SetName(cls.__name__)"?  If so, then the decorator approach
(with the line "self.name = None" removed) should be fine for your
purposes -- you'll just have the name stored in the class dict instead
of in each instance dict, but it will still be visible as long as you
haven't shadowed it.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »