python nested class

  • Thread starter Vedanta Barooah
  • Start date
V

Vedanta Barooah

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
 
R

Roland Heiber

Vedanta said:
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
>Exit code: 0

HtH, Roland
 
D

Daniel Dittmar

Vedanta said:
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
 
B

bruno modulix

Roland said:
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()
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 '(e-mail address removed)'.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 '(e-mail address removed)'.split('@')])"
 
G

George Sakkis

Daniel Dittmar said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,261
Messages
2,571,308
Members
47,976
Latest member
AlanaKeech

Latest Threads

Top