Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my Python class claim that I have 2 arguments instead of 1?

#! /usr/bin/env python
import os
import stat
import sys
class chkup:

        def set(file):
                filepermission = os.stat(file)
                user_read()
                user_write()
                user_exec()

        def user_read():
                """Return True if 'file' is readable by user 
            """
            # Extract the permissions bits from the file's (or
            # directory's) stat info.
                b = bool(filepermission.st_mode & stat.S_IRUSR)
                print b
            return b

        def user_write():
                """Return True if 'file' is readable by user 
            """
            # Extract the permissions bits from the file's (or
            # directory's) stat info.
                b = bool(filepermission.st_mode & stat.S_WRUSR)
                print b
            return b

        def user_exec():
                """Return True if 'file' is readable by user 
            """
            # Extract the permissions bits from the file's (or
            # directory's) stat info.
                b = bool(filepermission.st_mode & stat.S_IXUSR)
                print b
            return b

def main():
        i = chkup()
        place = '/net/home/f08/itsrsw1/ScriptingWork/quotacheck'
        i.set(place)

if __name__ == '__main__':
        main()

With that code I receive

> Traceback (most recent call last):
  File "chkup.py", line 46, in <module>
    main()
  File "chkup.py", line 43, in main
    i.set(place)
TypeError: set() takes exactly 1 argument (2 given)

Any thoughts?

like image 381
Jon Phenow Avatar asked Dec 08 '09 17:12

Jon Phenow


People also ask

How do you fix takes 1 positional argument but 2 were given?

To solve this ” Typeerror: takes 1 positional argument but 2 were given ” is by adding self argument for each method inside the class. It will remove the error.

What does takes 1 positional argument but 2 were given mean?

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.

How do you fix takes 2 positional arguments but 3 were given?

The do_math function takes two arguments but it gets called with 3. In this situation, we either have to update the function's declaration and take a third argument or remove the third argument from the function call.

What is __ add __ in Python?

Python __add__() function is one of the magic methods in Python that returns a new object(third) i.e. the addition of the other two objects. It implements the addition operator “+” in Python.


3 Answers

The first argument for a python class method is the self variable. If you call classInstance.method(parameter), the method is invoked as method(self, parameter).

So, when you're defining your class, do something like this:

class MyClass(Object): 
    def my_method(self, parameter): 
        print parameter

You might want to read through the Python tutorial.

like image 85
Seth Avatar answered Oct 16 '22 02:10

Seth


Because you're not passing the object (generally referred to as self) as the first parameter to your methods. In Python, a call like this:

my_obj.do_something(my_other_obj)

is essentially desugared into a call like this:

MyClass.do_something(my_obj, my_other_obj)

Thus, Python is looking for a method signature like this:

class MyClass(object):
    def do_something(self, my_other_obj):
        self.my_var = my_other_obj

So you should pass the object (generally called self) as the first parameter to a method.

like image 20
mipadi Avatar answered Oct 16 '22 02:10

mipadi


You need to explicitly pass self variable, which represents an instance of a class, e.g.:

def set(self, file):
    filepermission = os.stat(file)
    self.user_read()
    self.user_write()
    self.user_exec()

It doesn't have to be called self but it's a good convention to follow, and your code will be understood by other programmers.

like image 43
SilentGhost Avatar answered Oct 16 '22 03:10

SilentGhost