Missing 1 required positional argument

XallZall picture XallZall · Jul 22, 2014 · Viewed 86.8k times · Source

let me begin with that I am as green as it gets when it comes to programming but have been making process. My mind however still needs to fully understand why and what is happening.

Anyways the issue at hand as the Title suggests, I will paste the source code which I will keep to a minimum.

(I use Python Version 3.4.1)


class classname:
    def  createname(self, name):
        self.name = name;
    def displayname(self):
        return self.name;
    def saying(self):
        print("Hello %s" % self.name);

first = classname;
second = classname;

first.createname("Bobby");

I will paste here the error code:


Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
first.createname("Bobby")
TypeError: createname() missing 1 required positional argument: 'name'

I understand that I need to check the errors and read carefully what it says as well as do a search before a post, with that done and failing to solve the problem I come here to post it...

From here on you can read if you are interested about what I think is going on in my mind:

The error tells me that I need 1 more argument in the name, so I must be going wrong there, but I already tried something like this:

first.createname("bobby", "timmy");

Having more arguments in name? If I understand correctly an argument is in this --> () <--

I also rule out the fact that it would be the def createname(self, name), because self is or should be alone and not included? So I do not really understand what is going on.

Thank you in advance and sorry if this has been answered already, in which case I must have overlooked it.


SOLVED:

 first = classname;
 second = classname;

Should be:

 first = classname();
 second = classname();

The parentheses where missing which of course means I just made first and second EQUAL to something else and not link it with the "actual cl

Answer

Mike Williamson picture Mike Williamson · Oct 17, 2016

This is a very easy answer, surprised you never got a response! :(

You have not actually created an object yet.

For instance, you would want to write:

first = classname()

instead of just

first = classname

At the moment, how you wrote it, first is pointing to a class. E.g., if you ask what first is, you'd get:

<class '__main__.classname'>

However, after instantiating it (by simply adding the () at the end), you'd see that first is now:

<__main__.classname object at 0x101cfa3c8>

The important distinction here is that your call created a class, whereas mine created an object.

A class is to an object as humankind is to you, or as canine is to Lassie.

You created "canine", whereas you wanted to create "Lassie".


Note: you also usually want to initialize your objects. For that, you place an __init__ method in your class.