2

in need of a little insight. I have the following python code:

>>> class invader:
...     def __init__(self):
                // list
...             self.parameters = []
...
...     def parameters(self):
...             param = self.parameters
...             param.append('3')
... 
>>> invade = invader()
>>> invade.parameters()

Running this in terminal produces the following error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

How can I solve this?

1
  • 5
    Don't use parameters as both a member and property name. Commented Jul 2, 2015 at 22:58

4 Answers 4

3

You problem is using the same name for your attribute and method, rename self.parameters to self.param and use self.param in your method:

class invader:
   def __init__(self):
     self.param = []

   def parameters(self):
      self.param.append('3')

invade = invader()
invade.parameters()
print(invade.param)
Sign up to request clarification or add additional context in comments.

Comments

1

In the last line:

invade.parameters()

You are effectively using the list parameters as a function. (Note () at the end)

Do a

print invade.parameters 

will let you see the content of the list and remove the runtime error

Comments

1

Both your method and attribute contain the same name parameters so you can do as follows here:

def parameters(self):
    self._parameters.append('3')

It's a common to encapsulate attributes with underscores, especially with methods of the same name.

Comments

0

Your method and attribute contain the same name parameters. Since data attributes will override method attributes with the same name. So invade.parameters is list, not function. You should rename your function, such as append_parameters.
If you want to call parameters function, you can try this way:invader.parameters(invade).But it's not recommended

Comments

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.