Question

#! /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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

self is an implicit first argument to all class member functions. So the i.set(place) call actually calls set(i, place). You need to take this into account when defining your class, and write def set(self, file) instead.

set() is a method of class chkup. When you call i.set(place), python keeps track of the instance i using the first argument to the method. Generally, every instance method will receive at least one argument, called self, and subsequent arguments follow. You should redefine your class:

class chkup:
    def set(self, file):
        "etc..."

You might look up "self" and python on stackoverflow:

Python __init__ and self what do they do?

etc.

In a class, you need to take into account the self parameter for method members.

Since you're treating set as a bound (instance) method of a class, you must explicitly receive the instance as your first argument. It's called "self" by convention.

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

in order to define a non-static method you must provide "self" as a first argument like this

class chkup:

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

#this is done to make non static methods,

#the call of set() here done by

chk=chkup()

chk.set(fileName) # note that you dont provide "self" when calling

Thats because python automatically passes the current object as an argument to all the methods in the class,so when you pass 2 arguments to a function,python appends the third argument which is the current object,the method prototype should consider this

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top