Missing 1 required positional argument

I am as green as it gets when it comes to programming but have been making progress. My mind however still needs to fully understand what is happening.

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");

Error:

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

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");

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.


Solution 1:

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 set first as a class, whereas mine set it as an object.

Think about it like this: A class is to an object as humankind is to you, or as canine is to Lassie.

You set it as "canine", whereas you wanted to set it as "Lassie".


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