473,396 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

class closure question

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': [S] }
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
Jan 17 '08 #1
2 996
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': [S] }
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".
Jan 17 '08 #2
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': [S] }
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
Jan 17 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dave Benjamin | last post by:
I ran into an odd little edge case while experimenting with functions that create classes on the fly (don't ask me why): >>> def f(x): ... class C(object): ... x = x ... print C.x ......
27
by: Ted Lilley | last post by:
What I want to do is pre-load functions with arguments by iterating through a list like so: >>>class myclass: .... pass >>>def func(self, arg): .... print arg >>>mylist = >>>for item...
6
by: Kent Johnson | last post by:
Is there a way to persist a class definition (not a class instance, the actual class) so it can be restored later? A naive approach using pickle doesn't work: >>> import pickle >>> class...
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
9
by: User1014 | last post by:
I'm a javascript noob and have a question concerning some code I've come across that I'm trying to understand. Here is a snippet of the relevant section: (snip) var closure = this; var xhr =...
0
by: Gerard Brunick | last post by:
Consider: ### Function closure example def outer(s): .... def inner(): .... print s .... return inner .... 5
11
by: Huayang Xia | last post by:
What will the following piece of code print? (10 or 15) def testClosure(maxIndex) : def closureTest(): return maxIndex maxIndex += 5 return closureTest()
4
by: LAN MIND | last post by:
?
3
by: alan | last post by:
Hello all, I'm wondering if it's valid for a templated class to use a template parameter as a base class. Basically I have two abstract types, one of which is derived from the other, and I have...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.