1

I have a Class A in Python and I would like to populate the a static variable calling a static method like:

Class A:
   arr = []

   @staticmethod
   def FillArr():
       #do more stuff but for semplicity...
       A.arr = [[2,2,2,]]

  FillArr.__func__()

when I run the code I got 'NameError: name A not defined' so essentially I can't initialize the arr static variable. Essentially once the the class has been instantiated once I would like to populate the static variable

2 Answers 2

3

This runs flawlessly on Python 3.6:

class A:
   arr = []

   @staticmethod
   def fillArr():
       #do more stuff but for simplicity...
       A.arr = [[2,2,2,]]

A.fillArr()

print (A.arr)

Or, with the extra info in your comment:

class A:
    arr = []

    @staticmethod
    def fillArr():
        #do more stuff but for simplicity...
        A.arr = [[2,2,2,]]

    def __init__ (self):
        if not A.arr:
           A.fillArr ()

A ()

print (A.arr)
Sign up to request clarification or add additional context in comments.

4 Comments

ok I accepted but I needed to add a boolean to make it execute once
I've retested it, but on my system (win10, python3.6 it runs unmodified, printing [[2, 2, 2]]. Curious where you had to add the boolean...
yes because your code has only one instance of A , if you have multiple you should notice the diff
Ok, you want to populate it only once. Understand. Will edit.
2

Use @classmethod:

class A(object):
  arr = []

  @classmethod
  def FillArr(cls):
    cls.arr = [[2,2,2]]

A.FillArr()

print A.arr

This will result in: [[2,2,2]]

/edit/ the using normal method example I mention in my comment below (based on Jacques explanation):

class A
   arr=[]

   def FillArr(self):
     self.arr = [[2,2,2,]]

   def __init__(self):
     self.FillArr()

a = A()
print a.arr

4 Comments

nope.. I need to populate within the class not out of it... essentially once the the class has been instantiated once I would like to populate the static variable
@Jacques: lol I was already looking surprised when I suddenly saw the edit on my own post without doing a thing ;-)
@ai2016: What Jacques posts with his staticmethod solution can also done with classmethod. It is just what you like: Do you want to use the name of the class in the method explictly than use staticmethod, if instead you want to use cls (not to be confused with self, which is reserved for instanciated classes), use classmethod. But you can even do it with a normal method by replacing cls with self and call self.FillArr in init
To be honest I like the classmethod better, since it doesn't depend upon the name of the class, which may easily change, while the first param is by convention almost always named cls. Just took the question literally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.