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.si mplify()" would invoke Sum.simplify() and
reduce expression1 to 3*a+b+2
Question:
---------
Now suppose I set "expression 2 = Sum([a,-a])" and Sum.simplify()
recognises that the two terms cancel and the Sum has value 0.
Can I make "expression2.si mplify()" 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 = SimplifyFunctio n(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. 3 1485 an****@hotmail. com wrote: Now suppose I set "expression 2 = Sum([a,-a])" and Sum.simplify() recognises that the two terms cancel and the Sum has value 0.
Can I make "expression2.si mplify()" 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.sim plify()
if you don't care about the old value.
expression2 = SimplifyFunctio n(expression2)
This is another valid solution, but it's not an OO solution (you will
need to use "isinstance " to write "SimplifyFuncti on").
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
Dave Benjamin wrote: an****@hotmail. com wrote: Now suppose I set "expression 2 = Sum([a,-a])" and Sum.simplify() recognises that the two terms cancel and the Sum has value 0.
Can I make "expression2.si mplify()" 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.sim plify()
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.si mplify()" 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 = SimplifyFunctio n(expression2) This is another valid solution, but it's not an OO solution (you will
need to use "isinstance " to write "SimplifyFuncti on").
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 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.sim plify() 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.si mplify()" 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 dictionary with the given list of pairs?
|
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 from various sources, including webservices, that return that data
in their own objects. So to keep my application operating on the
objects it only knows, i need to transform the incoming data objects
into my app's data objects. important: The...
|
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 not work. It only works on lists and strings, for no obvious
reason. Why not on all sequence types?
Or, another example, the index() method has start and end parameters for
lists and strings. The count() method also has start and end parameters...
|
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. QuadMethod could be trapezoid,
romberg, etc... VarTrans could be None (no transform), TANH, IMT, ...
the client code should be able to provide an object of a certain class A
(not necessarily fixed which class, any class should do) and indicate what...
|
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. Also describes implementation
via ChangeManager (Mediator + Singleton)
2) Pattern hatching by John Vlissides
Describes Observer's implementation via Visitor Design Pattern.
3) Design Patterns Explained by Alan Shalloway and James Trott
| |
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 "database frontend"
type of intranet application. The existing table-display code consists
of a mountain of very clever but extremely brittle spaghetti-javascript,
which I'm planning to replace with XSLT transformations. At present I'm
still...
|
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 transform() function?
#include <algorithm>
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <string>
|
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 but seeing the slowdown in the number of postings, I'm hoping some of you will have more time. (And this is not some kind of coursework question!)
I obtain data elements from an asynchronous data source (i.e. the data arrives at irregular time...
|
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, buffer + size, buffer, tolower);
From the output, I find it works.
But when I compile it with Visual C++,
|
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: 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...
|
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...
|
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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |