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("ITes t cannot be instanciated")
def read(self,filen ame):
self.__filename = filename
def _getFilename(se lf):
return self.__filename
def make_reader():
return _PTest()
make_reader = staticmethod(ma ke_reader)
class _PTest(ITest):
class _PHandler(objec t):
def __init__(self):
super(_PHandler ,self).__init__ ()
#self.__map={}
def test(self):
pass
def __init__(self):
super(_PTest,se lf).__init__()
def read(self,filen ame):
super(_PTest,se lf).read(filena me)
print "HERE"
print dir()
print dir(self)
print self.__class__
print dir(self.__clas s__)
dh = self._PHandler( )
#dh.test()
if __name__=='__ma in__':
a=ITest.make_re ader()
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 1 1585
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(objec t):
def __init__(self): super(_PHandler ,self).__init__ ()
Make that
super(_PTest._P Handler, 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,se lf).__init__()
def read(self,filen ame): super(_PTest,se lf).read(filena me) print "HERE" print dir() print dir(self) print self.__class__ print dir(self.__clas s__) dh = self._PHandler( ) #dh.test()
if __name__=='__ma in__':
a=ITest.make_re ader() 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 were still an option.
However, I am coming up dry looking for information on how to do this, and
the most recent references I am able to find to nested classes in PHP are
dated 2003. And they all say the same thing: sorry, can't do that.
|
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 "Some code here"
class class2:
propA = "A"
def methB(self):
|
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:
|
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. This works fine if all of
the properties are at the top (root level) of the model but I'd like to keep
them in nested classes to organize them better.
So, for example, part of my data model looks like this (simplified) :
public class MainClass
|
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 Class(Steps) with a nested
Class(Questions) inside of it and a Collection of that Class(Steps) and it
works fine, but the Nested Collection(Questions) is escaping me.
Help!
| |
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
|
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 {
|
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 when a nested class inherits from
it’s parent class you get this infinite class chain in intellisense when
consuming the class. To get around this I created two child classes Reader
and Writer which require a base Person object.
When consuming...
|
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 ObjectEgg;
and have access to particular elements
|
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 binding of free variables. the scope in class statment is not a
closure, so there is only two possible scope in it : local and global.
When "class C2(C1):" statment is interpreted, it is in the scope of class B
for which a name C1 exists, but it...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |