473,626 Members | 3,183 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance and Inner/Nested Classes

I'm hoping that someone can explain why I get the following exception.
When I execute the code...

############### ############### ########
class Parent(object):
class Foo(object):
baz = 'hello from Parent.Foo'

class Child(Parent):
#Foo.baz = 'hello from Child.Foo'
pass

print Child.Foo.baz
print dir(Child)
############### ############### ########

....it displays what I expect...

hello from Parent.Foo
['Foo', '__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute __', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__' , '__repr__', '__setattr__', '__str__',
'__weakref__']

....but when I uncomment the first line of the Child class, Python
complains that Foo is undefined...

Traceback (most recent call last):
File "test1.py", line 5, in ?
class Child(Parent):
File "test1.py", line 6, in Child
Foo.baz = 'hello from Child.Foo'
NameError: name 'Foo' is not defined

Thanks in advance.

Paul

Jul 18 '05 #1
10 4540
Paul Morrow wrote:
############### ############### ########
class Parent(object):
class Foo(object):
baz = 'hello from Parent.Foo'

class Child(Parent):
#Foo.baz = 'hello from Child.Foo'
pass


Here you want "Parent.Foo .bax = ..." instead.

What are you actually trying to do? This is unusual code,
I think...

-Peter
Jul 18 '05 #2
Peter Hansen wrote:
Paul Morrow wrote:
############### ############### ########
class Parent(object):
class Foo(object):
baz = 'hello from Parent.Foo'

class Child(Parent):
#Foo.baz = 'hello from Child.Foo'
pass

Here you want "Parent.Foo .bax = ..." instead.

What are you actually trying to do? This is unusual code,
I think...

-Peter


I'm trying to override the 'baz' class attribute in the Child.Foo
subclass, so that when I do...

x1 = Parent.Foo()
x2 = Child.Foo()

.... x1 and x2 have different internal states (different values for the
baz attribute). Without nested classes, the following code...

############### ############### #######
class Parent(object):
baz = 'hello from Parent'

class Child(Parent):
baz = 'hello from Child'

print Parent.baz
print Child.baz
############### ############### #######

....produces what I would expect...

hello from Parent
hello from Child

....but apparently my thinking is wrong on nested class definitions. It
seems odd that, according to dir(Child), Foo is inherited by Child, but
I can't refer to Foo in Child's class definition. And even if I
could, it wouldn't be a *copy* of the Parent's Foo that Child has, but
rather Parent's Foo itself --- changing Foo.baz in Child changes Foo.baz
in Parent, which is not what I want.

Thx.

Jul 18 '05 #3
Paul Morrow wrote:
Peter Hansen wrote:
What are you actually trying to do? This is unusual code,
I think...
I'm trying to override the 'baz' class attribute in the Child.Foo
subclass, so that when I do...

x1 = Parent.Foo()
x2 = Child.Foo()

... x1 and x2 have different internal states (different values for the
baz attribute).


Sorry, let me try the question in a different way. What end
goal, without reference to *how* you think you want to achieve it,
are you trying to achieve? You've posted what are obviously
contrived examples. I can't see the purpose here... other
than "different internal states", but if that's all it is
you don't need to have an inner class to do it (as you show
you know by the following:)

Without nested classes, the following code...
############### ############### #######
class Parent(object):
baz = 'hello from Parent'

class Child(Parent):
baz = 'hello from Child'

print Parent.baz
print Child.baz
############### ############### #######

...produces what I would expect...

hello from Parent
hello from Child

...but apparently my thinking is wrong on nested class definitions. It
seems odd that, according to dir(Child), Foo is inherited by Child, but
I can't refer to Foo in Child's class definition. And even if I could,
it wouldn't be a *copy* of the Parent's Foo that Child has, but rather
Parent's Foo itself --- changing Foo.baz in Child changes Foo.baz in
Parent, which is not what I want.


Exactly... so what *do* you want?

By the way, have you considered just using *instances* instead
of mucking directly in the classes themselves? That's the usual
way to go about object-oriented programming (and probably why it's
not called "class-oriented programming" :-). If you did that, you
would use the initializer __init__() to set up the different state
by creating an instance of the Foo class inside the Parent's
initializer, then making the appropriate changes inside the
Child's initializer after calling the Parent's...

Another question is why bother with the nested class? Why
not just stick Foo outside both classes? (Doing this would
first require doing what I suggested in the previous paragraph,
however...but it might show you why what you are doing now
is sort of weird and maybe pointless.)

-Peter
Jul 18 '05 #4
On Mon, Jul 12, 2004 at 09:43:30AM -0400, Paul Morrow wrote:
Peter Hansen wrote:
Paul Morrow wrote:
############# ############### ##########
class Parent(object):
class Foo(object):
baz = 'hello from Parent.Foo'

class Child(Parent):
#Foo.baz = 'hello from Child.Foo'
pass

Here you want "Parent.Foo .bax = ..." instead.

What are you actually trying to do? This is unusual code,
I think...

-Peter


I'm trying to override the 'baz' class attribute in the Child.Foo
subclass, so that when I do...

x1 = Parent.Foo()
x2 = Child.Foo()

... x1 and x2 have different internal states (different values for the
baz attribute). Without nested classes, the following code...


I'm understanding what you want to do correctly, I guess you could do:

class Parent(object):
class Foo(object):
baz = 'hello from Parent.Foo'

class Child(Parent):
class Foo(Parent.Foo) :
baz = 'hello from Child.Foo'

i.e. Parent.Foo is a class that just happens to be stored in as an attribute
of Parent, and so if you want to override it, you have to do so directly.
The fact that you're thinking of them as "nested" doesn't come into it,
except for referencing it.

-Andrew.

Jul 18 '05 #5
Peter Hansen wrote:
Sorry, let me try the question in a different way. What end
goal, without reference to *how* you think you want to achieve it,
are you trying to achieve? You've posted what are obviously
contrived examples. I can't see the purpose here... other
than "different internal states", but if that's all it is
you don't need to have an inner class to do it (as you show
you know by the following:)


Actually, it's the *how* that is my end goal <grin>.

I'm creating a framework that you parameterize by supplying objects that
contain particularly named attributes. The attributes tell the
framework what to do, when to do it, etc. So I'm trying to find a nice
and clean object structure that uses a minimum of syntax.

The framework parameters (in the object the developer supplies) fall
into a number of categories, so it seemed like a good idea to group
them, hence the inner classes idea. And yes, since we are talking about
objects, a more traditional object-oriented layout such as...

############### ############### #######
class Parent(object):
class Foo(object):
def __init__(self):
self.baz = 'hello from Parent.Foo'

class Child(Parent):
class Foo(Parent.Foo) :
def __init__(self):
self.baz = 'hello from Child.Foo'

x1 = Parent.Foo()
x2 = Child.Foo()
print x1.baz
print x2.baz
############### ############### #######

....would work, but I was hoping to avoid having to create the
constructor methods as well as (re)declare the inner class in the
descendent/child classes. It looks like class attributes take care of
the former...

############### ############### #######
class Parent(object):
class Foo(object):
baz = 'hello from Parent.Foo'

class Child(Parent):
class Foo(Parent.Foo) :
baz = 'hello from Child.Foo'

x1 = Parent.Foo()
x2 = Child.Foo()
print x1.baz
print x2.baz
############### ############### #######

.... but I don't see any way to avoid the later.

I know that this is still a contrived example, but just imagine that the
Foo inner class represents some customization category, and baz is a
customization variable in that category.

Thanks again.

Jul 18 '05 #6
Paul Morrow wrote:
I'm creating a framework that you parameterize by supplying objects that
contain particularly named attributes. The attributes tell the
framework what to do, when to do it, etc. So I'm trying to find a nice
and clean object structure that uses a minimum of syntax.

The framework parameters (in the object the developer supplies) fall
into a number of categories, so it seemed like a good idea to group
them, hence the inner classes idea. And yes, since we are talking about
objects, a more traditional object-oriented layout such as...


Thanks for the clarification. Based on what you are saying
here, I strongly suggest abandoning the idea of keeping
these attribute classes as inner classes. I think doing that
couples together two unrelated things and will cause much
more trouble in the long run than the modest reduction in
extra typing will save you.

The objects that are being parameterized (the ones the developer
supplies) are not the same thing as the parameterizatio n objects.
The former are for the developer, and presumably have something
to do with the problem domain (whatever the framework is helping
the developer solve).

The parameterizatio n objects, however, are *for* the framework,
not for the problem domain (if I'm understanding correctly)
and shouldn't be coupled with the other ones.

Even if they appear to be related (maybe because the types of
parameterizatio n involved come directly from the problem
domain as well), I would still keep them very separate. Otherwise
you are mixing together the whole parameterizatio n feature
with the rest of the system and the result will probably grow
unmanageable or at least complicated in due time.

-Peter
Jul 18 '05 #7
On Mon, Jul 12, 2004 at 10:28:08AM -0400, Paul Morrow wrote:
[...]

...would work, but I was hoping to avoid having to create the
constructor methods as well as (re)declare the inner class in the
descendent/child classes. It looks like class attributes take care of
the former...

############### ############### #######
class Parent(object):
class Foo(object):
baz = 'hello from Parent.Foo'

class Child(Parent):
class Foo(Parent.Foo) :
baz = 'hello from Child.Foo'

x1 = Parent.Foo()
x2 = Child.Foo()
print x1.baz
print x2.baz
############### ############### #######

... but I don't see any way to avoid the later.

I know that this is still a contrived example, but just imagine that the
Foo inner class represents some customization category, and baz is a
customization variable in that category.


Well, the problem is that the way you say you want spell it is mutating a
shared object:

class Child(Parent):
Foo.baz = 'hello from Child.Foo'

If you want 'Foo' in Child to be a different object to the Foo in Parent,
then you need to somehow make it be a different object, which means
assigning to 'Foo' in Child's namespace. The simplest and clearest way to
do that is to explicitly subclass with "class Foo(Parent.Foo) : ...".

-Andrew.

Jul 18 '05 #8
Peter Hansen wrote:
The objects that are being parameterized (the ones the developer
supplies) are not the same thing as the parameterizatio n objects.


I'm not quite sure what you mean by "parameterizati on objects" as
opposed to what the developer creates. Note that there are two kinds of
developers here: one that works on the Framework; and one that builds
applications using the Framework. It's the later that I've been talking
about.

For each application, the application developer creates an object that
describes the application. This object contains a bunch of settings that
customize the Framework. For any given app, there are a lot of possible
settings, where the settings can be grouped into a number of categories
(security, style, structure, content, etc.). I could just say that the
namespace inside of the object (the app developer supplies) is flat, but
that would get messy fast. It seems that it would be better to store
related attributes together in their own namespace inside of the object.
That's what I imagined using the inner classes for.

Jul 18 '05 #9
Andrew Bennetts wrote:

Well, the problem is that the way you say you want spell it is mutating a
shared object:

class Child(Parent):
Foo.baz = 'hello from Child.Foo'

If you want 'Foo' in Child to be a different object to the Foo in Parent,
then you need to somehow make it be a different object, which means
assigning to 'Foo' in Child's namespace. The simplest and clearest way to
do that is to explicitly subclass with "class Foo(Parent.Foo) : ...".

-Andrew.


Yes, I think that you may be right. But oh the tempation to look into
metaclasses for this... :)

Thanks.

Jul 18 '05 #10

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

Similar topics

5
4331
by: devu | last post by:
Currently I'm working on a class that performs a batch process as per scheduling for all employees in a company. Being a batch process load is obviously a huge factor. The process first needs to calculate the eligibility of each employee before proceeding to the next stage. I need to cache some of the employee information if an employee is found eligible for later use in the next stage. One option could be a javabean style value object...
15
26419
by: Bob | last post by:
I need to apply a border to a table's columns, but not to the columns of tables nested within (I realize nested tables are bad form, it's a client request). I can't control what goes inside this table so applying a class to any nested tables that cancels out this border isn't an option. Is there any way to prevent the inheritance?
4
4011
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best Regards KInd --
6
2075
by: Marco | last post by:
Howdy! Given: public abstract class A { public abstract int A1(int i); private class B { private int B1(int i) { int j;
22
23346
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
3
2059
by: Martin Skou | last post by:
I'm experimenting with using Python for a small web interface, using Mark Hammond's nice win32 extensions. I use a small class hierarchy which uses inheritance and nested classes. Here are a small extract of the code: class page: def __init__(self):
4
1331
by: news.microsoft.com | last post by:
Hello, I've got a design problem that I can't figure out. I need to override a static method which happens to be referenced by methods inside nested classes. In the sample below, in the call tst.runTest() I need the result to display "child", however it always displays "parent". I can understand all the reasons why it should display "parent" and not "child" but that doesn't help me solve it. In my real-world problem the parent...
21
2475
by: raylopez99 | last post by:
Well, contrary to the implication in my 2000 textbook on C# (public beta version), C# does allow multiple inheritance, so long as it's serially chained as follows: class derived02 : derived01 { } class derived01 : baseclass01
3
2076
by: ChrisW | last post by:
Hiya, So I have a class that creates threads within it. These threads are a class underneath the parent class. I want to access values in the parent class from the threads while they run. Yet this gives me an error: cannot access a non-static member of outer type 'Parent.Class' via nested type 'Parent.Class.Thread'. So how do I access values in the parent class?
0
8196
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,...
1
8364
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8504
tracyyun
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...
0
7193
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
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...
0
5574
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();...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2625
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
1
1808
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.