473,699 Members | 2,834 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can you create an instance of a subclass with an existing instance of the base class?

Can you create an instance of a subclass using an existing instance of
the base class?

Such things would be impossible in some languages or very difficult in
others. I wonder if this can be done in python, without copying the
base class instance, which in my case is a very expensive object.

Any ideas?

Thanks,
-Sandra

Apr 21 '06
18 7386

Lawrence D'Oliveiro wrote:

All you want is a dictionary, then. That's basically what Python objects
are.


Yes, that's it exactly. I made a lazy wrapper for it, and I was really
happy with what I was able to accomplish, it turned out to be very
easy.

Thanks,
-Sandra

Apr 24 '06 #11
Sandra-24 a écrit :
Lawrence D'Oliveiro wrote:
In article <11************ *********@v46g2 000cwv.googlegr oups.com>,
"Sandra-24" <sa***********@ yahoo.com> wrote:

Now that is a clever little trick. I never would have guessed you can
assign to __class__, Python always surprises me in it's sheer
flexibilit y.
That's because you're still thinking in OO terms.


It's not quite as simple as all that. I agree that people, escpecially
people with a Java (ew) background overuse OO, when there's often
simpler ways of doing things.


Nope. I mean : they don't overuse OO, they overuse *classes*. AFAIK, OO
means *object* oriented - not class oriented. There are OO languages
that don't even have a notion of class.
However in this case I'm simply getting an object (an mp_request object
from mod_python) passed into my function, and before I pass it on to
the functions that make up and individual web page it is modified by
adding members and methods to add functionality.
Which is a well-known design pattern called "decorator" .

(snip) Sadly I'm unable to create it as a python object first, because it's
created by the time my code comes into play. So I have to resort to
using the new module to add methods.
This is OK IMHO.
It works, but it has to be redone for every request,
Is this really a problem ?
I thought moving
the extra functionality to another object would simplify the task.

A
better way might be to contain the mp_request within another object and
use __getattr__ to lazily copy the inner object. I'd probably have to
first copy those few fields that are not read-only or use __setattr__
as well.


Why copy ? You could as well just use composition/delegation (also using
__getattr__ - and BTW, this is another possible implementation of the
decorator pattern).

Apr 24 '06 #12
In article <44************ ***********@new s.free.fr>,
bruno at modulix <on***@xiludom. gro> wrote:
Lawrence D'Oliveiro wrote:
(snip)
I think you're taking Python's OO-ness too seriously. One of the
strengths of Python is that it can _look_ like an OO language without
actually being OO.


According to which definition of OO ?


Isn't there one?
Apr 25 '06 #13
Lawrence D'Oliveiro wrote:
In article <44************ ***********@new s.free.fr>,
bruno at modulix <on***@xiludom. gro> wrote:

Lawrence D'Oliveiro wrote:
(snip)
I think you're taking Python's OO-ness too seriously. One of the
strengths of Python is that it can _look_ like an OO language without
actually being OO.


According to which definition of OO ?

Isn't there one?


Your claim that Python "_look_ like an OO language without actually
being OO" implicitely relies on a definition of OO - or is just
meaningless. So I ask you: what definition of OO do you use to support
your claim ?

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'on***@xiludom. gro'.split('@')])"
Apr 25 '06 #14
In article <44************ **********@news .free.fr>,
Bruno Desthuilliers <bd************ *****@free.quel quepart.fr> wrote:
Sandra-24 a écrit :
Lawrence D'Oliveiro wrote:
In article <11************ *********@v46g2 000cwv.googlegr oups.com>,
"Sandra-24" <sa***********@ yahoo.com> wrote:
Now that is a clever little trick. I never would have guessed you can
assign to __class__, Python always surprises me in it's sheer
flexibility .

That's because you're still thinking in OO terms.
It's not quite as simple as all that. I agree that people, escpecially
people with a Java (ew) background overuse OO, when there's often
simpler ways of doing things.


Nope. I mean : they don't overuse OO, they overuse *classes*. AFAIK, OO
means *object* oriented - not class oriented.


Oh great. Now we have someone redefining the concept of OO to evade the
point I was making.
There are OO languages that don't even have a notion of class.


Sounds like stuff I was doing in C (a non-OO language) years ago. Unless
you want to count C as an OO language, I think you're going to have to
retract this claim.
However in this case I'm simply getting an object (an mp_request object
from mod_python) passed into my function, and before I pass it on to
the functions that make up and individual web page it is modified by
adding members and methods to add functionality.


Which is a well-known design pattern called "decorator" .


If you have to think of it as a design pattern, that means you haven't
figured out the code reuse angle yet.
Apr 27 '06 #15
In article <44************ **********@news .free.fr>,
bruno at modulix <on***@xiludom. gro> wrote:
Lawrence D'Oliveiro wrote:
In article <44************ ***********@new s.free.fr>,
bruno at modulix <on***@xiludom. gro> wrote:

Lawrence D'Oliveiro wrote:
(snip)

I think you're taking Python's OO-ness too seriously. One of the
strengths of Python is that it can _look_ like an OO language without
actually being OO.

According to which definition of OO ?


Isn't there one?


Your claim that Python "_look_ like an OO language without actually
being OO" implicitely relies on a definition of OO - or is just
meaningless.


Which nicely evades answering the question.
Apr 27 '06 #16
Lawrence D'Oliveiro wrote:
In article <44************ **********@news .free.fr>,
Bruno Desthuilliers <bd************ *****@free.quel quepart.fr> wrote:

Sandra-24 a écrit :
Lawrence D'Oliveiro wrote:
In article <11************ *********@v46g2 000cwv.googlegr oups.com>,
"Sandra-24" <sa***********@ yahoo.com> wrote:

>Now that is a clever little trick. I never would have guessed you can
>assign to __class__, Python always surprises me in it's sheer
>flexibilit y.

That's because you're still thinking in OO terms.

It's not quite as simple as all that. I agree that people, escpecially
people with a Java (ew) background overuse OO, when there's often
simpler ways of doing things.
Nope. I mean : they don't overuse OO, they overuse *classes*. AFAIK, OO
means *object* oriented - not class oriented.

Oh great. Now we have someone redefining the concept of OO to evade the
point I was making.


"redefining " ? lol...
There are OO languages that don't even have a notion of class.

Sounds like stuff I was doing in C (a non-OO language) years ago. Unless
you want to count C as an OO language, I think you're going to have to
retract this claim.


I think I'm not going to retract anything. And I think you should learn
a bit more about prototype-based languages.
However in this case I'm simply getting an object (an mp_request object
from mod_python) passed into my function, and before I pass it on to
the functions that make up and individual web page it is modified by
adding members and methods to add functionality.


Which is a well-known design pattern called "decorator" .


If you have to think of it as a design pattern, that means you haven't
figured out the code reuse angle yet.


Please stop saying non-sense and learn the difference between design and
implementation.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Apr 27 '06 #17
In <ld************ ***********@lus t.ihug.co.nz>, Lawrence D'Oliveiro wrote:
In article <44************ **********@news .free.fr>,
Bruno Desthuilliers <bd************ *****@free.quel quepart.fr> wrote:
Nope. I mean : they don't overuse OO, they overuse *classes*. AFAIK, OO
means *object* oriented - not class oriented.


Oh great. Now we have someone redefining the concept of OO to evade the
point I was making.
There are OO languages that don't even have a notion of class.


Sounds like stuff I was doing in C (a non-OO language) years ago. Unless
you want to count C as an OO language, I think you're going to have to
retract this claim.


That sounds like stuff you do in a language that has objects but no
classes. As C has no objects I would not count it as an OO language. But
I count Io as an OO language::

#!/usr/bin/env io
Foo := Object clone
Foo value := 42
Foo setValue := method(newValue , self value = newValue; self)
Foo beep := method("beep" linePrint)
Foo asString := method("I'm a Foo. My value is " .. self value)

Bar := Foo clone
Bar asString := method("I'm a Bar and " .. super asString)

foo := Foo clone
foo beep
foo asString linePrint

bar := Bar clone setValue(23)
bar beep
bar asString linePrint

Output is:

beep
I'm a Foo. My value is 42
beep
I'm a Bar and I'm a Foo. My value is 23

That's OO IMHO. Clonable objects that know their "ancestors" so you can
build an object hierarchy. Missing attributes are looked up in the
"ancestors" and one can explicitly look up the inheritance tree with
``super``. There are no classes, just four objects. Convention in naming
and usage makes two of them something like templates or "classes" for new
objects but they are in no way special.

Ciao,
Marc 'BlackJack' Rintsch
Apr 28 '06 #18
Lawrence D'Oliveiro wrote:
In article <44************ **********@news .free.fr>,
bruno at modulix <on***@xiludom. gro> wrote:

Lawrence D'Oliveiro wrote:
In article <44************ ***********@new s.free.fr>,
bruno at modulix <on***@xiludom. gro> wrote:

Lawrence D'Oliveiro wrote:
(snip)
>I think you're taking Python's OO-ness too seriously. One of the
>strength s of Python is that it can _look_ like an OO language without
>actually being OO.

According to which definition of OO ?

Isn't there one?


Your claim that Python "_look_ like an OO language without actually
being OO" implicitely relies on a definition of OO - or is just
meaningless .

Which nicely evades answering the question.


Well I have to say you are also nicely evading answering the question,
which is enough to make me suspect your are trolling (deliberately
asking contentious questions for the purposes of creating futile
argument and discussion).

If you *aren't* trolling then what's your objection to saying what led
you to make the assertion that Python could look like an OO language
without being one?

But sine you say later that "Python objects are basically dictionaries"
it's clear your understanding of Python isn't terribly complete, which
might cast doubt on your understanding of object orientation.

For the record, Python *is* an object-oriented language, but it happens
to offer convenient features for procedural programming as well. Since
these features are orthogonal to its OO features, the fact that they
exist doesn't stop Python from being an OO language.

So why do you assert that it "merely looks like" one?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Apr 29 '06 #19

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

Similar topics

0
1240
by: Stephen Nesbitt | last post by:
All: Here's my implementation problem. I have a base class which has the responsibility for providing entry into the logging system. Part of the class responsibility is to ensure that lagger names are consistent. For all intents and purposes this class should be considered abstract and will always be subclassed. What I want to do is the following: - allow the logger name to set explicitly. I've accomplished this by
37
2830
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily use the instance of this base class (or its children class). How can I ensure the instance IS-A base class instance, since Python is a fully dynamic typing language? I searched and found several different ways to do this:
4
1644
by: Joe HM | last post by:
Hello - I have a Base Class where I want a New() implemented that can be called from the outside. This New() should create an instance of the appropriate cDerivedX Class ... The following shows a scenario where it was determined that cDerivedA is needed based on a configuration file. Dim lInstance As New cBase()
8
1674
by: Lou Pecora | last post by:
I've been scanning Python in a Nutshell, but this seems to be either undoable or so subtle that I don't know how to do it. I want to subclass a base class that is returned from a Standard Library function (particularly, subclass file which is returned from open). I would add some extra functionality and keep the base functions, too. But I am stuck. E.g.
1
4995
by: s.lipnevich | last post by:
Hi All, Is anything wrong with the following code? class Superclass(object): def __new__(cls): # Questioning the statement below return super(Superclass, cls).__new__(Subclass) class Subclass(Superclass): pass
5
3169
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a instance is created by "calling" a class object.
5
4468
by: Sergio Montero | last post by:
I have a MustInherits Base class that implements a custom IDataLayer interfase. IDataLayer expose CRUD methods. Base class constructor requires two parameters: ConnectionString TableName Another assembly, sharing the root namespace, contains a set of Custom attributes used to validate properties values, just like the Validation Application Block.
6
4594
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast would solve the problem, but it appears not to. I need this functionality to make object serialization a reality. Having to explicitly cast each deserialized object to its original type defeats the purpose of my serializing the blasted things in...
4
3473
by: Kurt Smith | last post by:
Hi List: Class inheritance noob here. For context, I have the following base class and subclass: class Base(object): def __init__(self, val): self.val = val
0
8685
marktang
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...
0
9171
Oralloy
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...
0
9032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7743
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
6532
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
5869
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
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
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
2
2342
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.