-3

I have a class and I want to initialize some variables and then use them in other functions.

class TamAccConnect2: 
    def __init__( self ):
        self.deviceName = []
    def func1(self):
        device1 = 567;
        self.deviceName = 56756
        self.device = device1;
        print('No, you called me!')
    def ReadCurrentData():
        errorCode = 1;
        return errorCode;

>>> TamAccConnect2.func1()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: func1() missing 1 required positional argument: 'self'

Why should I get an error like this, I have created other classes in a similar manner and they've been fine.

Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
Voltage Spike
  • 162
  • 4
  • 15
  • @millimoose: didn't the OP show that in the transcript, at the end? – DSM Oct 14 '13 at 22:38
  • @DSM The transcript is an edit by Games Brainiac, it was less clear in the original version of the post. – millimoose Oct 14 '13 at 22:40
  • recommended reading: http://www.python.org/dev/peps/pep-0008/ – Erik Kaplun Oct 14 '13 at 22:47
  • There's no reason to put a semicolon at the end of a line in Python. – jwodder Oct 14 '13 at 22:48
  • 1
    @ErikAllik: While PEP 8 is useful for any Python developer, I'm not sure how it would help here. He's using PEP 8 naming conventions for the class, the `self` parameter, and the `func1` method name, and 4-space indentation, and so on. It's true that he's got _other_ places where he violates PEP 8, but the code has bigger problems than that… – abarnert Oct 14 '13 at 22:49
  • 1
    @abarnert: he also has a method named `ReadCurrentData` and is using semicolons and extraneous spaces. – Erik Kaplun Oct 14 '13 at 22:50
  • @ErikAllik: "It's true that he's got _other_ places where he violates PEP 8, but the code has bigger problems than that…" – abarnert Oct 14 '13 at 23:02
  • Possible duplicate of [TypeError: Missing 1 required positional argument: 'self'](https://stackoverflow.com/questions/17534345/typeerror-missing-1-required-positional-argument-self) – tk421 Aug 25 '17 at 22:51

1 Answers1

3

You can't call an instance method on the class itself; you have to create an instance, then call the method on that instance:

>>> connect2 = TamAccConnect2()
>>> connect2.func1()

You're going to have a similar, but opposite, problem when you try to call that other method, ReadCurrentData. Because you didn't give it a self parameter, when you try to call it on an instance, you'll get a TypeError about too many arguments, instead of too few. (Also, it looks like you might have been expecting ReadCurrentData to update a class, instance, or global variable, instead of just creating a local variable that goes away as soon as the method is done.)

While we're at it, if this is Python 2.x, you should always define new-style classes by explicitly inheriting from object—e.g., class TamAccConnect2(object):.

It's hard to explain all of this better in an SO answer than in a tutorial, so please re-read the official tutorial's section on Classes, or whichever other tutorial or text you're learning from.


If you've created other classes in a similar manner, they weren't fine. You may have gotten away with things because of other errors that cancelled this one out (e.g., you passed an argument when you shouldn't have, and the method never used its self, or used it in such a way that it happened to work with the argument you passed), but it isn't actually working as intended.


If you're wondering exactly why the error message looks like this, it's a bit complicated. I've tried to write a gentle explanation here, but I suspect it's still hard to understand. Anyway, the short version is that you are not passing an argument to match the self parameter, and you're not calling a bound method which would automatically pass the object it's bound to as an argument to match the self parameter, so you get an error saying you're missing the self argument.

abarnert
  • 354,177
  • 51
  • 601
  • 671