On Sunday, 9 October 2016 04:25:39 UTC+11, Emmanuel Charpentier wrote:
>
> I wonder if it is possible to patch a new method in an existing class. For 
> example, it might be handy to give SR methods for doing what can be 
> obtained by Maxima's function demoivre and exponentialize. Those methods 
> would be wrappers for the (existing) functions maxima.demoivre and 
> maxima.exponentialize, along the lines of 
> return(maxima.demoivre(self).sage() (ditto for exponentialize).
>

Yes this is possible, although possibly python purists will object! 

When I need/want to do this I use the following decorator:

Andrew
def add_method_to_class(klass):
  r""" 
  Add a new method to a pre-existing (sage) class.

  """
  def add_func(func):
    setattr(klass,func.func_name,func)
    return func

  return add_func


The decorator takes the class that you want to add the method to as an 
argument.

As a silly example of usage, the following lines add a method to the 
Partition class:

@add_method_to_class(Partition):
def new_partition_method(self):
    r"""
    Usual sage/python documentation
    """
    return sum(self)





-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to