class closure question

Discussion in 'Python' started by Steven W. Orr, Jan 17, 2008.

  1. I want to indirectly change the value of a variable.

    #! /usr/bin/python
    foo = [44]
    bar = foo
    bar[0] = 55
    print 'bar = ', bar
    print 'foo = ', foo

    This works fine.

    bar = [55]
    foo = [55]

    But I want to do the same with a class value.

    #! /usr/bin/python
    S = None
    dd = { 'class': }
    class C1(object):
    def __init__(self):
    print 'Hello from C1'

    def mkclass(base):
    class zzz(base):
    pass
    return zzz

    dd['class'][0] = mkclass( C1 )
    print "dd['class'][0].__bases__ = ", dd['class'][0].__bases__
    print 'S = ', S

    The answer is not what I want:

    dd['class'][0].__bases__ = (<class '__main__.C1'>,)
    S = None

    The goal is for S to be set to the returned class from mkclass.

    Can someone help?

    --
    Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
    happened but none stranger than this. Does your driver's license say Organ ..0
    Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
    individuals! What if this weren't a hypothetical question?
    steveo at syslang.net
     
    Steven W. Orr, Jan 17, 2008
    #1
    1. Advertising

  2. Steven W. Orr

    Peter Otten Guest

    Steven W. Orr wrote:

    > I want to indirectly change the value of a variable.
    >
    > #! /usr/bin/python
    > foo = [44]
    > bar = foo
    > bar[0] = 55
    > print 'bar = ', bar
    > print 'foo = ', foo
    >
    > This works fine.
    >
    > bar = [55]
    > foo = [55]
    >
    > But I want to do the same with a class value.
    >
    > #! /usr/bin/python
    > S = None
    > dd = { 'class': }
    > class C1(object):
    > def __init__(self):
    > print 'Hello from C1'
    >
    > def mkclass(base):
    > class zzz(base):
    > pass
    > return zzz
    >
    > dd['class'][0] = mkclass( C1 )
    > print "dd['class'][0].__bases__ = ", dd['class'][0].__bases__
    > print 'S = ', S
    >
    > The answer is not what I want:
    >
    > dd['class'][0].__bases__ = (<class '__main__.C1'>,)
    > S = None
    >
    > The goal is for S to be set to the returned class from mkclass.
    >
    > Can someone help?


    What you want is not possible in Python. You can modify some objects
    (called "mutable") but rebinding a name has to be explicit.

    Peter
     
    Peter Otten, Jan 17, 2008
    #2
    1. Advertising

  3. Steven W. Orr a écrit :
    > I want to indirectly change the value of a variable.
    >


    Are you sure this is the correct formulation of your problem ?

    > #! /usr/bin/python
    > foo = [44]
    > bar = foo
    > bar[0] = 55
    > print 'bar = ', bar
    > print 'foo = ', foo
    >
    > This works fine.
    >
    > bar = [55]
    > foo = [55]
    >
    > But I want to do the same with a class value.
    >
    > #! /usr/bin/python
    > S = None
    > dd = { 'class': }
    > class C1(object):
    > def __init__(self):
    > print 'Hello from C1'
    >
    > def mkclass(base):
    > class zzz(base):
    > pass
    > return zzz
    >
    > dd['class'][0] = mkclass( C1 )


    Hem... If your goal is to rebind S, then you got it all wrong. What
    you're doing here is to rebind dd['class'][0] from S to the return value
    of mkclass. This won't of course rebind S itself.

    > print "dd['class'][0].__bases__ = ", dd['class'][0].__bases__
    > print 'S = ', S
    >
    > The answer is not what I want:
    >
    > dd['class'][0].__bases__ = (<class '__main__.C1'>,)
    > S = None
    >
    > The goal is for S to be set to the returned class from mkclass.


    Seems like you still don't get how Python's assignment work.

    FWIW, the type of object you want to bind to S is totally irrelevant, so
    you could get rid of this mkclass stuff for the moment. Also, you don't
    have to make dd['class'] point to a list containing S here - dicts are
    themselves mutables, so your example would work the same with

    dd = {'class':S}

    and
    dd['class'] = any_other_object;


    > Can someone help?


    Not me, at least unless you explain your real use case - I mean, the
    problem you're trying to solve by "indirectly chang(ing) the value of a
    variable".
     
    Bruno Desthuilliers, Jan 17, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ted Lilley

    lambda closure question

    Ted Lilley, Feb 19, 2005, in forum: Python
    Replies:
    27
    Views:
    766
    Karl Anderson
    Feb 22, 2005
  2. Replies:
    10
    Views:
    533
    Terry Reedy
    Mar 30, 2005
  3. Gerard Brunick

    Where does a class closure live?

    Gerard Brunick, Dec 6, 2006, in forum: Python
    Replies:
    0
    Views:
    266
    Gerard Brunick
    Dec 6, 2006
  4. Steven W. Orr
    Replies:
    3
    Views:
    259
    James Stroud
    Feb 22, 2007
  5. Julian Mehnle
    Replies:
    0
    Views:
    258
    Julian Mehnle
    Jul 17, 2003
Loading...

Share This Page