Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing 1 required positional argument

Tags:

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.

like image 696
XallZall Avatar asked Jul 22 '14 19:07

XallZall


People also ask

How do you fix missing positional arguments in Python?

The Python "TypeError: missing 2 required positional arguments" occurs when we forget to provide 2 required arguments when calling a function or method. To solve the error, specify the arguments when calling the function or set default values for the arguments.

Is self a positional argument Python?

The Python "TypeError: missing 1 required positional argument: 'self'" occurs when we call a method on the class instead of on an instance of the class. To solve the error, instantiate the class first and call the method on the instance, e.g. emp1. get_name() . Here is an example of how the error occurs.

What does takes 1 positional argument but 2 were given?

The Python "TypeError: takes 1 positional argument but 2 were given" occurs for multiple reasons: Forgetting to specify the self argument in a class method. Forgetting to specify a second argument in a function's definition. Passing two arguments to a function that only takes one.

What is the example of positional argument?

An example of positional arguments can be seen in Python's complex() function. This function returns a complex number with a real term and an imaginary term. The order that numbers are passed to the complex() function determines which number is the real term and which number is the imaginary term.

What is missing 1 required positional argument in Python?

Python missing 1 required positional argument: ‘self’ Solution. Python classes need to be instantiated, or called, before you can access their methods. If you forget to instantiate an object of a class and try to access a class method, you encounter an error saying “missing 1 required positional argument: ‘self’”.

Why is my positional argument missing from my class?

The “missing 1 required positional argument: ‘self’” error can occur when you incorrectly instantiate a class. Consider the following code: While this code is similar to our solution from the last example, it is incorrect. This is because we have not added any parenthesis after the word Hero.

What is the required positional argument in JavaScript?

missing 1 required positional argument: ‘self’ Positional arguments refer to data that is passed into a function. In a class, every function must be given the value “ self ”. The value of “self” is similar to “this” in JavaScript. “self” represents the data stored in an object of a class.

What is a positional argument in Python programming?

What is a "positional argument" in Python programming? Short answer: A positional argument is any argument that's not supplied as a key=value pair. To understand what that means, unfortunately, is somewhat involved. The term "argument" is used somewhat imprecisely throughout the programming community and especially in Python documentation.


1 Answers

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.

like image 54
Mike Williamson Avatar answered Oct 26 '22 23:10

Mike Williamson