2

Im working on computer algebra and im trying to make an interface for fields. Trying not to bore you with all the details this is whath im trying to get working:

class PrimeField():
  prime = 2
  def __init__(self,p):
     self.prime = p

  class element():
     def __init__(self,n):
        self.descriptor = n

     def someFunctionOfNandP(self):
         return 2*self.n % self.parrent.p

  field = PrimeField(7)
  el = field.element(5)
  print(el.someFunctionOfNandP())

What im trying to achieve is that PrimeField creates a generator as you will that makes elements (which are objects) who's functions depend on the field the came from. Although I can think of workarounds that use different structures (for example making a general element class that takes two elements and a function in the field that inits these), something among the lines shown above would have preference due to its resemblance to the underlying mathematics and the program is written for somebody else who is a mathematician.

Can this be done, and if so, how?

Thank you

  • What relationship does ```element``` have to ```PrimeField``` that requires it to be defined within ```PrimeField```'s code block? – wwii Dec 03 '14 at 03:48
  • Still trying to wrap my head around this - but maybe one of the answers to [Inner Classes: How can I get the outer-class object at construction time?](http://stackoverflow.com/questions/2278426/inner-classes-how-can-i-get-the-outer-class-object-at-construction-time) will help. O maybe something with a ```__metaclass__```. – wwii Dec 03 '14 at 04:23
  • Lots of interesting ideas/answers with a `nested classes python` search - other than "don't do it" that is. http://stackoverflow.com/a/1765716/2823755 – wwii Dec 03 '14 at 04:42
  • Did you mean ```def someFunctionOfNandP(self): return 2*self.descriptor % self.parrent.prime```? – wwii Dec 03 '14 at 05:52
  • Does `elementfield` need access to `PrimeField` class attributes or bound instance attributes? – wwii Dec 04 '14 at 05:34
  • Possible duplicate of [Inner Classes: How can I get the outer-class object at construction time?](http://stackoverflow.com/questions/2278426/inner-classes-how-can-i-get-the-outer-class-object-at-construction-time). After playing around a bit, the answer to that question is the answer to this one. – wwii Dec 05 '14 at 05:24

1 Answers1

0

One possibility is to treat PrimeField and PrimeFieldElement as separate objects:

class PrimeFieldElement():

    def __init__(self, p, n):
        self.p = p
        self.n = n

    def someFunctionOfNandP(self):
         return 2 * self.n % self.p


class PrimeField():
    prime = 2

    def __init__(self, p):
        self.prime = p

    def element(self, n):
        return PrimeFieldElement(self.prime, n)

field = PrimeField(7)
el = field.element(5)
print(el.someFunctionOfNandP())

This seems to match what you want best: each call to .element() will produce a new element and it'll have the values of p and n stored in it. It's also possible to pass the PrimeField along with it and access it inside PrimeFieldElement:

def element(self, n):
    return PrimeFieldElement(self, n)

together with:

class PrimeFieldElement():

    def __init__(self, field, n):
        self.field = field
        self.n = n

    def someFunctionOfNandP(self):
         return 2 * self.n % self.field.prime
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180