Skip to content Skip to sidebar Skip to footer

Can A Class Variable Be An Instance Of The Class?

Can a class variable of say, class Foo be a Foo object itself? For example, I'm trying to build a class for the finite field of order 11, and I want a chosen generator (2) to be as

Solution 1:

You can do something like this:

classFiniteField11:def__init__(self, element):
       self.elt = element
FiniteField11.generator = FiniteField11(2)

Your code fails because FiniteField11 was not defined when the class defintion was parsed.

Solution 2:

Yes it can, but the name doesn't exist until the class statement finishes. Therefore, you have to set this class variable after creating the class, perhaps just below the class block or in the instance initializer.

Post a Comment for "Can A Class Variable Be An Instance Of The Class?"