operator over loading

A

Anand K Rayudu

Hi all,

I have defined q python class and want to know if i can over load '='
operator.

Here is my scenario

Def myClass:
__init__(self,a): # a is C Object
c_obj = a

__del__
delete c object.

b = myClass(c_obj1)
c = myClass(c_obj2)

#python expression
a=tan(sin(b)*cos(c))

Where sin, cos & tan and '*' are over loaded, and the result of this is
assigned to variable a.

Now the problem is cos(c), sin(b) creates new objects, which are used
for multiplication.
This again generates a new object,
What i want to do it automatically delete all intermediate objects.
except the one assigned to a.
currently i am deleteing c object in __del__
So once I reassing some thing else to a that is also getting deleted.
So what i want to do is once i assign it to a variable, i want to set
flag so that i won;t delete it in _del_

Can some some suggest is there any better way of doing the same,
Thanks & Best Regards.
Anand
 
D

Diez B. Roggisch

Anand said:
Hi all,

I have defined q python class and want to know if i can over load '='
operator.

No you can't - that stems from the fact that assignment in python means that
a value is bound to <name>, not that there is made a copy of that value
stored into some space referred to by <name>, as it is done it C/C++. Think
of variables as references. There is also no &= operator in C++ to
overload.
Now the problem is cos(c), sin(b) creates new objects, which are used
for multiplication.
This again generates a new object,
What i want to do it automatically delete all intermediate objects.
except the one assigned to a.
currently i am deleteing c object in __del__
So once I reassing some thing else to a that is also getting deleted.
So what i want to do is once i assign it to a variable, i want to set
flag so that i won;t delete it in _del_

Why do you care? Let pythons GC do the job for you.

It looks like you try to impose C++ thinking on python here. While thats
understandable, its never good to try to stick to the idioms of one
language in anotherone - you miss the nice differences then :)
 
P

Peter Maas

Anand said:
I have defined q python class and want to know if i can over load '='
operator.

You mean assignment? You can't overload that because ist is not
function like (doesn't return a value).

If you mean comparison (==) then you want the __eq__() method.
Details see Python Manual 3.3.1 Basic customization.

If you seek for a copy constructor, have a look at copy.deepcopy
in Python Manual 3.18.

Mit freundlichen Gruessen,

Peter Maas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,199
Messages
2,571,045
Members
47,644
Latest member
EarnestK61

Latest Threads

Top