python - Having trouble with the self parameter -


I am working on a small project and I am not sure if I am getting an error or whatever I am doing wrong.

I am using OOP in the Python running on Wing IDLE. I have the latest version of Python Shell running on Windows 8 PC

In my program, I have a method that takes the user input and uses that input, so that it is necessary to create the shelf The subject_name = input ("Enter the name of the subject:") subject_file = topic_name + "file" name = topic_name return subject_name, subject_file, name '

   < P> Ideally this program then made three returning statements Abuse will, namely Visy_nam, Visy_fail, and name to open a 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 is created") 'is correct, while: print ("" "1. Create a new note. Add new word 3. View word 4.Exit / Quit" "") ans = input ("What will you do If this happens: ") 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 the choice 1, which will run the code above So I have, I get calls error. & lt; Module & gt; Buildins.TypeError: subject_creator () 1 Required Positional Logic Disappeared: 'Self'

This is somewhat surprised because I include the self parameter when I have mentioned the above for the producer Besides the code I wrote, I have no other errors.

Any feedback will be highly appreciated.

You are confused with "function" and "method"

In Python, a method is defined in a class field, and this object will be received as its first argument:

  Class examples: def try_me (self): print "hello."   

You will use it like this:

  x = example (x.try_me ()   

and Its first (here, neglected) logic as try_me () will receive x . This is useful so that the method can be used to use properties of the object instance etc.

Contrast this with regular functions, i.e. out of class : defined:

  def try_me_too (): print " Hello. "   

Which is implemented just like

  try_me_too ()   

Tanz, your example code does not raise The value returned by your subject_creator function:

  & gt; If ans == "1": & gt; Subject only () & gt; Note = Notebook (subject_name, subject_file, name)   

An area where is not named variable named etc. You need to create it in some way. If ans == "1": ick, bar, poo = topic_creator () note = notebook (ick, bar, poo)

(I The chosen nonsense variable names mainly emphasize that they are different from the defined variables, and are only available inside subject matter.


Simply complete it For, here is an exhibit how self is useful.

  class oteraxapple: def __init __ (self, greeting): self.greeting = greeting def try_me_also (self): print self.greeting   

Use from:

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

Here, greeting is the property of an object created by us ; The __init __ method gets it as its argument, and stores it as an example of an example. Uses try_me_also method self to fetch this feature, and prints it.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -