I literally wrote the following at the top of my module:

#\lambda^(i) from Carrell-Goulding paper
def i_part(self,i):
    if i==0:
        return self
    elif i<0:
        return (self.conjugate().i_part(-i)).conjugate()
    zero_one_sequence = self.zero_one_sequence()
    num_ones = zero_one_sequence.count(1)
    if num_ones < i:
        zero_one_sequence += (i-num_ones)*[1]
    index_one_list = [index for index in range(len(zero_one_sequence)) if 
zero_one_sequence[index] == 1]
    zero_one_sequence[index_one_list[i-1]]=0
    return Partitions().from_zero_one(zero_one_sequence)

Partition.i_part = i_part

Then if later I wrote:

p = Partition([3,2,1])

I can call 

p.i_part(2)


This works. I just want to know whether there is a better or more standard 
solution.

When you say a new __init__ would be good, how do you mean to implement 
that?

Dinakar


On Thursday, June 5, 2014 1:11:36 PM UTC-4, kcrisman wrote:
>
>
>> I want to know the best practices for extending the functionality of a 
>> sage class. For example, I would like to add the following method to the 
>> Partition class in sage:
>>
>> #\lambda^(i) from Carrell-Goulding paper
>> def i_part(self,i):
>>     if i==0:
>>         return self
>>     elif i<0:
>>         return (self.conjugate().i_part(-i)).conjugate()
>>     zero_one_sequence = self.zero_one_sequence()
>>     num_ones = zero_one_sequence.count(1)
>>     if num_ones < i:
>>         zero_one_sequence += (i-num_ones)*[1]
>>     index_one_list = [index for index in range(len(zero_one_sequence)) if 
>> zero_one_sequence[index] == 1]
>>     zero_one_sequence[index_one_list[i-1]]=0
>>     return Partitions().from_zero_one(zero_one_sequence)
>>
>>
>> My first inclination is to subclass Partition and add this as a method to 
>> the subclass. However, I am not able to make this work.
>>
>>
> What exactly did you write?  A new initialize (__init__) would be a good 
> first step.
>  
>
>> So what I have done so far is to just include the above function 
>> definition in a module followed by the following line:
>>
>> Partition.i_part = i_part
>>
>> So I am dynamically changing the Partition class. This doesn't seem like 
>> a good practice in general, so I would like to know what is the preferred 
>> solution.
>>
>> Dinakar
>>
>

-- 
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 http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to