473,386 Members | 1,745 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,386 software developers and data experts.

nested class problem

Hello all,

I am trying to play with nested class in a script I am making,
and I am not sure I really understand how they work.
Here is some code:

__all__ = ["ITest"]

from exceptions import Exception

class ITest(object):
def __init__(self):
if (self.__class__ == ITest):
raise Exception("ITest cannot be instanciated")

def read(self,filename):
self.__filename = filename

def _getFilename(self):
return self.__filename

def make_reader():
return _PTest()
make_reader = staticmethod(make_reader)

class _PTest(ITest):

class _PHandler(object):

def __init__(self):
super(_PHandler,self).__init__()
#self.__map={}

def test(self):
pass

def __init__(self):
super(_PTest,self).__init__()
def read(self,filename):
super(_PTest,self).read(filename)
print "HERE"
print dir()
print dir(self)
print self.__class__
print dir(self.__class__)
dh = self._PHandler()
#dh.test()

if __name__=='__main__':

a=ITest.make_reader()
print dir(a)
b=a.read("")
print b


I want to put class _PHandler in _Ptest,
and use _PHandler in _Ptest methods,
but anything I try doesnot work...

On this configuration, I have:
Traceback (most recent call last):
File "./test.py", line 58, in ?
b=a.read("")
File "./test.py", line 51, in read
dh = self._PHandler()
File "./test.py", line 34, in __init__
super(_PHandler,self).__init__()
NameError: global name '_PHandler' is not defined

and I have similar problems if I try to access _PHandler
in different ways...
Is this possible somehow ? Or what is the problem with my approach ?
I know I could just dont use nested classes.., but I'd like to try.

Thanks in advance.

Regards,


--
Stephane Ninin

Jul 18 '05 #1
1 1567
Stephane Ninin wrote:
I am trying to play with nested class in a script I am making,
and I am not sure I really understand how they work.
[..]
class _PTest(ITest):

class _PHandler(object):

def __init__(self):
super(_PHandler,self).__init__()
Make that
super(_PTest._PHandler, self).__init__()

or, even better, omit it entirely. Python class hierarchies tend to remain
flat, because inheritance is only necessary for code reuse, not to indicate
a particular interface.
#self.__map={}

def test(self):
pass

def __init__(self):
super(_PTest,self).__init__()
def read(self,filename):
super(_PTest,self).read(filename)
print "HERE"
print dir()
print dir(self)
print self.__class__
print dir(self.__class__)
dh = self._PHandler()
#dh.test()

if __name__=='__main__':

a=ITest.make_reader()
print dir(a)
b=a.read("")
print b


I want to put class _PHandler in _Ptest,
and use _PHandler in _Ptest methods,
but anything I try doesnot work...
I think that's you fighting against the language and - a rare case - python
fighting back :-)

On this configuration, I have:
Traceback (most recent call last):
File "./test.py", line 58, in ?
b=a.read("")
File "./test.py", line 51, in read
dh = self._PHandler()
File "./test.py", line 34, in __init__
super(_PHandler,self).__init__()
NameError: global name '_PHandler' is not defined

and I have similar problems if I try to access _PHandler
in different ways...
Is this possible somehow ? Or what is the problem with my approach ?
I know I could just dont use nested classes.., but I'd like to try.


I fail to see the benefit of nested classes. If you want to bring some
structure into your python application, try packages instead.

Peter
Jul 18 '05 #2

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

Similar topics

26
by: Joshua Beall | last post by:
Hi All, I remember reading that both nested classes and namespaces would be available in PHP5. I know that namespaces got canceled (much sadness...), however, I *thought* that nested classes...
9
by: OKB (not okblacke) | last post by:
For a variety of reasons, I'm interested in putting together some code that will allow me to created structures out of nested classes, something like: class class1: def methA(self): print...
3
by: Erik Bongers | last post by:
Hi, Nested classes only seem to be able to access static members of the surrounding class : class SurroundingClass { public: class InnerClass { public:
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
5
by: Atley | last post by:
I am trying to set up a nested collection so I can run statements like this: me.lblquestions.text = mysteps.item(1).questions(1).Asked Any ideas on how to do this? I have created a...
7
by: Ryan Shaw | last post by:
I’m having a small problem with inheritance with a hierarchy of classes The example is Class Class Private m_classB as Class Class Class End Clas End Clas
1
by: Tomas Sieger | last post by:
Hi all, I'm in doubt with the following code: class Base { public: class Nested {}; }; class Derived:public Base { public: class Nested {
2
by: miked | last post by:
I am architecting in a read only class for use in mapping data to a business object. The object makes strong use of nested classes and their ability to access protected fields. The downside is...
5
by: ZikO | last post by:
Hi there. I have a problem. I have created nested classes but don't know how to access to inner classes. I know I can create objects: Hen Object; Hen::Nest ObjectNest; Hen::Nest::Egg...
0
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 11:29:18 Cousson, Benoit, vous avez écrit : This is a language limitation. This is because nested scope is implemented for python function only since 2.3 allow late...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.