#! /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?
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With