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

OO design question / Transform object in place?

Hello comp.lang.py,

Can you help me with ideas for the following (somewhat newbie) OO
design question in Python? Note, I'm using psuedo-code, not actual
Python for the examples!

Background:
-----------
I need to represent a small variety of mathematical constructs
symbolically using Python classes.

1) I have classes, representing numbers, symbolic variables etc.
2) I have classes for groups of these objects, such as a symbolic Sums.
3) To simplify the mathematical constructs, I've provided each of the
classes with a simplify(self) method.

The following psuedo-code gives an example

a = Variable('a')
b = Variable('b')
two = Number(2) #Yes there are good reason not
#to just use Python's int object! :-)

expression1 = Sum([a,b,a,a,two]) #This represents a+b+a+a+2

then calling "expression1.simplify()" would invoke Sum.simplify() and
reduce expression1 to 3*a+b+2

Question:
---------

Now suppose I set "expression2 = Sum([a,-a])" and Sum.simplify()
recognises that the two terms cancel and the Sum has value 0.

Can I make "expression2.simplify()" transform expression2 from an
instance of Sum to an instance of Number(0) **in place**? Is that
possibe, or do I really have to write

expression2 = SimplifyFunction(expression2)

and use the function return to set expression2 to be a Number(0)
object, which is annoying for a variety of reasons! Have I made a
mistake in the design?

Many thanks,
andy.

Jul 19 '05 #1
3 1472
an****@hotmail.com wrote:
Now suppose I set "expression2 = Sum([a,-a])" and Sum.simplify()
recognises that the two terms cancel and the Sum has value 0.

Can I make "expression2.simplify()" transform expression2 from an
instance of Sum to an instance of Number(0) **in place**? Is that
possibe, or do I really have to write
I think it's much better for simplify() to return a new object always,
and leave the original object unmodified. You can still write:

expression2 = expression2.simplify()

if you don't care about the old value.
expression2 = SimplifyFunction(expression2)
This is another valid solution, but it's not an OO solution (you will
need to use "isinstance" to write "SimplifyFunction").
and use the function return to set expression2 to be a Number(0)
object, which is annoying for a variety of reasons! Have I made a
mistake in the design?


It's usually considered poor OO style to have an object change its class
at runtime, although I'm sure you could do it in Python ("__bases__"
would be a place to start) if you really wanted. For what you're trying
to do, I don't think it's necessary, though.

Dave
Jul 19 '05 #2
Dave Benjamin wrote:
an****@hotmail.com wrote:
Now suppose I set "expression2 = Sum([a,-a])" and Sum.simplify()
recognises that the two terms cancel and the Sum has value 0.

Can I make "expression2.simplify()" transform expression2 from an
instance of Sum to an instance of Number(0) **in place**? Is that
possibe, or do I really have to write
I think it's much better for simplify() to return a new object

always, and leave the original object unmodified. You can still write:

expression2 = expression2.simplify()
Dave,

A belated thank-you message for your reply to my posting. I took your
advice, and made all the simplify methods return new objects and this
has simplified my code structure a great deal (and any slow down in run
time just doesn't matter!).

Am I right that to make "expression2.simplify()" return a new object I
will need use copy.copy a lot, as in:

def simplify(self):
newreturnvalue=copy.copy(self)
#
#....now the code does lots of complicated things
# to newreturnvalue object...
#
return newreturnvalue

I have a nagging doubt as to whether this is what you meant, or if I've
missed a trick again.

Anyway, thanks again for your reply.

Yours,
Andy.

if you don't care about the old value.
expression2 = SimplifyFunction(expression2)
This is another valid solution, but it's not an OO solution (you will

need to use "isinstance" to write "SimplifyFunction").
and use the function return to set expression2 to be a Number(0)
object, which is annoying for a variety of reasons! Have I made a
mistake in the design?
It's usually considered poor OO style to have an object change its

class at runtime, although I'm sure you could do it in Python ("__bases__"
would be a place to start) if you really wanted. For what you're trying to do, I don't think it's necessary, though.

Dave


Jul 19 '05 #3
an****@hotmail.com wrote:
Dave Benjamin wrote:
I think it's much better for simplify() to return a new object
and leave the original object unmodified. You can still write:
expression2 = expression2.simplify()
A belated thank-you message for your reply to my posting. I took your
advice, and made all the simplify methods return new objects and this
has simplified my code structure a great deal (and any slow down in run
time just doesn't matter!).


No problem. Glad it helped. =)
Am I right that to make "expression2.simplify()" return a new object I
will need use copy.copy a lot, as in:

def simplify(self):
newreturnvalue=copy.copy(self)
#
#....now the code does lots of complicated things
# to newreturnvalue object...
#
return newreturnvalue


You could use copy.copy(), as long as you want a shallow copy and you
don't want to change from one class to another. However, it may be to
your advantage to use the constructor of one or more classes to build
the result; this allows you to conditionally pick the result object's
class. Also, there may be situations where an object can't be
simplified, in which case you can just return "self", and avoid copying
entirely.

Dave
Jul 19 '05 #4

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

Similar topics

22
by: lechequier | last post by:
Let's say I define a list of pairs as follows: >>l = Can anyone explain why this does not work? >>h = {}.update(l) and instead I have to go: >>h = {} >>h.update(l) to initialize a...
4
by: farseer | last post by:
i'm looking for the right solution to my problem. i have an app that uses certain core set of data object that it knows about and operates on. The source of the actually data however may come...
105
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does...
1
by: Gert Van den Eynde | last post by:
hi, i'm writing a numerical quadrature class and could use some help on the design. a numerical quadrature needs two "algorithms": the quadrature method and (possibly) a variable transform....
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
6
by: Pete Verdon | last post by:
Summary: Can I do an XSLT transform, in the client, of a tree of nodes taken from the displayed page DOM in IE? This works in Firefox. Hi, I'm just starting the process of rewriting part of a...
8
by: Alex Buell | last post by:
I've just written the below as an exercise (don't worry about the lack of checking), but I was wondering why I needed to write a struct with an operator() as a parameter to supply to the STL...
4
by: arnaudk | last post by:
I am trying to come up with a class design to deal with asynchronous data to be stored and analyzed over multiple time frames and could really use some design input. This is a rather long question...
2
by: Lambda | last post by:
I'd like to change all the character in a array to lower case. I find transform() is convenient. Because the array is large, I don't want to define another array. I tried: transform(buffer,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
0
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...
0
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,...

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.