0

I am working on a small project and I am not sure if the error I am getting is due to the IDLE I am using or something I am doing wrong.

I am using OOP in python running on the Wing IDLE. I have the latest version of the python shell running on a Windows 8 PC.

In my program I have a method that takes user input and using that input it creates the parameters required to create a shelf.

'def subject_creator(self): 
    subject_name = input("Enter the subject name:") 
    subject_file = subject_name + "file"  
    name = subject_name 
    return subject_name, subject_file, name' 

Ideally the program would then use the three returned statements namely subject_name, subject_file, and name in opening the new shelf.

 'def __init__(self, subject_name,  subject_file, name ): 
    subject_name = shelve.open ("subject_file", "c") 
    self.name = name  
    print("The", self.name ,"note has been created")' 

 while True:
    print ("""
    1.Create new note
    2.Add new term 
    3.Look up term 
    4.Exit/Quit
    """)
    ans=  input("What would you like to do?: ") 
    if ans=="1":  
            subject_creator() 
            note = Notebook(subject_name, subject_file, name) 
            subject_name.sync() 

However when I run the program and in my main menu I select choice 1 which runs the code above, I receive and error that states.

<module>
builtins.TypeError: subject_creator() missing 1 required positional argument: 'self' 

This is somewhat puzzling as I include the self parameter when I wrote the code for subject creator as shown above. Other than this I have no other errors.

Any feedback would be greatly appreciated.

2 Answers2

2

You are confusing "function" and "method".

In Python, a method is a function defined inside a class scope, and it will receive the object as its implicit first argument:

class Example:
    def try_me(self):
        print "hello."

You would use it like this:

x = Example()
x.try_me()

and try_me() will receive x as its first (here, ignored) argument. This is useful so that the method can access the object instance's attributes etc.

Contrast this with a regular function, i.e. one defined outside of a class:

def try_me_too():
    print "hello."

which is simply invoked like

try_me_too()

Tangentially, your example code does not pick up the values returned by your subject_creator function:

>    if ans=="1":  
>        subject_creator() 
>        note = Notebook(subject_name, subject_file, name)

The scope where this happens doesn't have variables named subject_name etc. You need to create them somehow.

if ans=="1":
    ick, bar, poo = subject_creator()
    note = Notebook(ick, bar, poo)

(I chose nonsense variable names mainly to emphasize that these are different from the variables defined, and only available, inside subject_creator.)


Just to complete this, here is a demonstration of how self is useful.

class Otherexample:
    def __init__(self, greeting):
        self.greeting = greeting
    def try_me_also(self):
        print self.greeting

use like this:

y = Otherexample("hello.")
y.try_me_also()

Here, the greeting is a property of the object we created; the __init__ method receives it as its argument, and stores it as an attribute of the instance. The try_me_also method uses self to fetch this attribute, and print it.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thank you, I just thought you have to use self whenever you create a method inside of a class. Didn't know the true reasoning about it. – user3353958 Apr 25 '14 at 01:51
0

This line causes your error

subject_creator()

You should change

def subject_creator(self): 
    subject_name = input("Enter the subject name:") 
    subject_file = subject_name + "file"  
    name = subject_name 
    return subject_name, subject_file, name

to

def subject_creator(): 
    subject_name = raw_input("Enter the subject name:") 
    subject_file = subject_name + "file"  
    name = subject_name 
    return subject_name, subject_file, name

You don't need a parameter in your function if you won't use it.

Also,if you are using python 2.x, consider using raw_input() and not input(). For more info, please read differences between input() and raw_input().

Community
  • 1
  • 1
Rod Xavier
  • 3,983
  • 1
  • 29
  • 41