views:

195

answers:

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

Thanks in advance

+1  A: 

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.

Wim
A: 

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

jldupont
+17  A: 

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.

Seth
Thanks all, I forgot about that. This the first actual class I've created with Python, so thanks for the patience. I started on Java so currently this feels pretty different!
jphenow
Yeah, I remember having similar issues when I first started working with Python classes (not to mention being slightly annoyed that I had to explicitly write "self" everywhere). The hardest part about going python is learning to forget all of the cruft that you learned from Java or C++ :)
Seth
+2  A: 

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.

SilentGhost
That should be **self**.user_read(), **self**.user_write() etc.
atzz
Thanks, atzz, of course it's only correct for the same-class methods
SilentGhost
A: 

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()
Jonathan Feinberg
+4  A: 

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.

mipadi
+1  A: 

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:

http://stackoverflow.com/questions/625083/python-init-and-self-what-do-they-do

etc.

Noah
A: 

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

Ahmad Dwaik
A: 

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

appusajeev