operator over loading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anand K Rayudu

    operator over loading

    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)*co s(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



  • Diez B. Roggisch

    #2
    Re: operator over loading

    Anand K Rayudu wrote:
    [color=blue]
    > Hi all,
    >
    > I have defined q python class and want to know if i can over load '='
    > operator.[/color]

    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.
    [color=blue]
    > 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_[/color]

    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 :)

    --
    Regards,

    Diez B. Roggisch

    Comment

    Working...