0

So, it says that create in line 11 is not defined, but it is a recursive function within the class. And In VS Code, it shows me an error at line 6 - it says that I am missing the self argument, but when I add it, it requires 3 arguments in line 23 (why, I can't provide an argument for self, can I ?)

I already tried it with various variations of adding self to the arguments, but nothing worked.

class smarray:
    def __init__ (self):

        self.array = []

    def create(index, dim):
        array1 = []
        if index < len(dim)-1:
            for x in range(0,dim[index]):

                array1.append((create(index+1,dim)))

            return array1
        else:
            for x in range(0,dim[index]):
                array1.append("nul")
            return array1
        if index ==0:
            self.array = array1

t = smarray()

t = smarray.create(0, [3,4])

error TB:

Traceback (most recent call last):
  File "/Users/pc/Documents/VS Code Files/Python testing/testing range.py", line 23, in <module>
    t = smarray.create(0, [3,4])
  File "/Users/pc/Documents/VS Code Files/Python testing/testing range.py", line 11, in create
    array1.append((create(index+1,dim)))
NameError: name 'create' is not defined
2
  • you forgot self: def create(self, index, dim): It is unclear what you are trying to do with this class: you create an instance t, then you reassign it to the result of a method that returns None... Maybe call t.create(...) instead. Commented Aug 17, 2019 at 21:22
  • self if missing from parameters in your method. Try this: def create(self, index, dim):... and when you want to call it then use your instance (t) Like this: t.create(0, [3,4]) Commented Aug 17, 2019 at 21:39

1 Answer 1

3

There are a couple of things needed to be fixed in order for this code snippet to run:

class smarray:
    def __init__(self):
        self.array = []

    def create(self, index, dim):
        array1 = []
        if index < len(dim)-1:
            for x in range(0, dim[index]):
                array1.append((self.create(index+1, dim)))
            return array1
        else:
            for x in range(0, dim[index]):
                array1.append("nul")
            return array1

        if index == 0:
            self.array = array1


t = smarray()
my_array = t.create(0, [3, 4])

So, the first fix would be adding the self keyword to the def create() method signature.

Second, in the line array1.append(...) the same self keyword had to be added, so we can call the create method properly: self.create()

And the last one I changed the call to the create method as "instance method" and not as "class method" - I hope I understood correctly what you tried to achieve here. You can read more here.

Pay attention that the last if index==0 is unreachable, thus the code self.array = array1 won't be ever executed. I don't quite sure what you were trying to achieve there.

Sign up to request clarification or add additional context in comments.

3 Comments

You may want to point out what you fixed to make your answer more useful and complete.
@FedericoklezCulloca, thank you, I will edit my answer accordingly!
I wanted to make a class in which you can create a array with a custom number of "dimensions" and custom "length of those dimensions" automatically. Thanks

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.