470,631 Members | 1,595 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,631 developers. It's quick & easy.

python nested class

greetings....

in a python nested class is it possible to change the value of the
parent class's variable without actually creating an instance of the
parent class, consider this code:

class mother:
x=0
def __init__(self):
self.x=1
def show(self):
print self.x
class child:
def increase(self,num):
# mother.increase=num
o = mother()
o.show()
y=mother.child()
y.increase(20)
# this should print 20
o.show()

...... is it possible somehow ???

thanks and regards,
vedanta
Jul 21 '05 #1
4 4245
Vedanta Barooah wrote:
o = mother()
o.show()
y=mother.child()
y.increase(20)
# this should print 20
o.show()

...... is it possible somehow ???
Hi,

this should do what you want:

--- test.py
class mother:
x=0
def __init__(self):
mother.x=1
def show(self):
print mother.x
class child:
def increase(self,num):
mother.x=num

o = mother()
o.show()
y=mother.child()
y.increase(20)
# this should print 20
o.show()

---pythonw -u "test.py" 1
20Exit code: 0


HtH, Roland
Jul 21 '05 #2
Roland Heiber wrote:
Vedanta Barooah wrote:
o = mother()
o.show()
y=mother.child()
y.increase(20)
# this should print 20
o.show()

...... is it possible somehow ???

Hi,

this should do what you want:

--- test.py
class mother:
x=0
def __init__(self):
mother.x=1
def show(self):
print mother.x
class child:
def increase(self,num):
mother.x=num

o = mother()
o.show()
y=mother.child()
y.increase(20)
# this should print 20
o.show()


---
pythonw -u "test.py"

1
20


This may *not* be what the op want...
#--- test2.py

class mother(object):
x=0
def __init__(self, name):
self.name = name
mother.x=1
def show(self):
print "in %s: %d" % (self.name, mother.x)
class child(object):
def increase(self,num):
mother.x=num

o = mother('o')
o.show()
y=mother.child()
y.increase(20)
# this should print 20
o.show()

o2 = mother('o2')
o2.show()
y2=mother.child()
y2.increase(10)
o2.show()
o.show()
--
bruno desthuilliers
ruby -e "print 'o****@xiludom.gro'.split('@').collect{|p|
p.split('.').collect{|w| w.reverse}.join('.')}.join('@')"
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 21 '05 #3
Vedanta Barooah wrote:
in a python nested class is it possible to change the value of the
parent class's variable without actually creating an instance of the
parent class


Python nested classs are like *static* Java nested classes. Non-static
Java classes are very different in that they have an implicit reference
to an instance of the enclosing class. This is why you can instantiate
non-static inner classes only in the context (= non-static method) of a
specific object. There is no direct equivalent to this in Python, so you
have to do the steps yourself.

- the constructor takes an additional argument, the 'outer' object,
which has to be kept in the object:
def __init__ (self, outer, ...):
self.outer = outer

- when creating the inner object, the outer object must be passed to the
constructor
obj = InnerClass (self)

- the outer object must be explicitely referenced:
self.outer.increase (20)

Daniel
Jul 21 '05 #4
"Daniel Dittmar" <da************@sap.corp> wrote:
Vedanta Barooah wrote:
in a python nested class is it possible to change the value of the
parent class's variable without actually creating an instance of the
parent class


Python nested classs are like *static* Java nested classes. Non-static
Java classes are very different in that they have an implicit reference
to an instance of the enclosing class. This is why you can instantiate
non-static inner classes only in the context (= non-static method) of a
specific object. There is no direct equivalent to this in Python, so you
have to do the steps yourself.

- the constructor takes an additional argument, the 'outer' object,
which has to be kept in the object:
def __init__ (self, outer, ...):
self.outer = outer

- when creating the inner object, the outer object must be passed to the
constructor
obj = InnerClass (self)

- the outer object must be explicitely referenced:
self.outer.increase (20)

Daniel


Or you can automate these steps and make implicit the reference to the
outer object using a descriptor:

#====== Test =============================================

def test():
class Outer:
def __init__(self,x): self.x = x

# if python gets class decorators someday,
# an inner class could be specified simply by:
#@innerclass
class Inner:
def __init__(self, y): self.y = y
def sum(self): return self.x + self.y
# as of python 2.4
Inner = innerclass(Inner)

outer = Outer(1)
inner = outer.Inner(2)
assert inner.sum() == 3
# outer.x, inner.x, inner.__outer__.x refer to the same object
outer.x = 4; assert inner.sum() == 6
inner.x = 10; assert inner.sum() == 12
inner.__outer__.x = -1; assert inner.sum() == 1
# an inner class must be bounded to an outer class instance
try: Outer.Inner(0)
except AttributeError, e: pass #print e
else: assert False

#================================================= ======

def innerclass(cls):
'''Class decorator for making a class behave as a Java (non-static)
inner class.

Each instance of the decorated class is associated with an instance
of its enclosing class. The outer instance is referenced implicitly
when an attribute lookup fails in the inner object's namespace. It
can also be referenced explicitly through the property '__outer__'
of the inner instance.
'''
if hasattr(cls, '__outer__'):
raise TypeError('Existing attribute "__outer__" '
'in inner class')
class InnerDescriptor(object):
def __get__(self, outer, outercls):
if outer is None:
raise AttributeError('An enclosing instance that '
'contains %s.%s is required' %
(cls.__name__, cls.__name__))
clsdict = cls.__dict__.copy()
# explicit read-only reference to the outer instance
clsdict['__outer__'] = property(lambda s: outer)
# implicit lookup in the outer instance
clsdict['__getattr__'] = lambda s,attr: getattr(outer,attr)
def __setattr__(this, attr, value):
# setting an attribute in the inner instance sets the
# respective outer instance if and only if the
# attribute is already defined in the outer instance
if hasattr(outer, attr): setattr(outer,attr,value)
else: super(this.__class__,this).__setattr__(attr,
value)
clsdict['__setattr__'] = __setattr__
return type(cls.__name__, cls.__bases__, clsdict)
return InnerDescriptor()
Regards,
George

Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Timothy Fitz | last post: by
10 posts views Thread by Andrew Dalke | last post: by
14 posts views Thread by Michele Simionato | last post: by
14 posts views Thread by Mark Dufour | last post: by
reply views Thread by Kurt B. Kaiser | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.