#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    88
    Rep Power
    10

    trouble with calling OOP instance


    I haven't posted here in a while..
    Im having some trouble with calling an Instance in a program I wrote that allows the user to view and add info to elements.
    Here's the code:
    Code:
    #Created by Pinko (C) 2005
    #Periodic Python project started Feb. 10, 2005
    #Protected under the GNU Public Liscens
    #Visit me on the web @ http://pinko.zetasystems.com
    import os
    print "Welcome to Periodic Python"
    print "Version 0.01"
    print "~~~~~~~~~~~"
    
    class PerTbl:
    	def addelement(self):
    		element = raw_input("What is the name of this element? ")
    		pernum = raw_input("What is its atomic number? ")
    		atmwei = raw_input("What is its atomic weight? ")
    		prioxinum = raw_input("What is its primary oxidation number? ")
    		symbol = raw_input("What is its symbol?[No Caps] ")
    		file = "//"+symbol+".txt"
    		sto = open(file,'a')
    		sto.append(element+"/n",pernum+"/n",atmwei+"/n",prioxinum+"/n",symbol+"/n")
    		sto.close()
    	def searchelement(self):
    		query = raw_input("Please enter the atomic symbol of the element you which to get information on")
    		search = "//"+query+".txt"
    		dir = getcwd()
    		if searchfile in dir:
    			searchfile = open(search,'r')
    			info = searchfile.readlines()
    			print info
    			searchfile.close()
    		else:
    			print "I'm sorry, that element is not in our database currently"
    running = 1
    while running == 1:	
    	rc = 0
    	while rc == 0:
    		print "What would you like to do?"
    		print "1- Get Info on an element"
    		print "2- Add an element"
    		print "3- Exit"
    		choice = raw_input("> ")
    		rc = 1
    	if choice == "1":
    		PerTbl.searchelemet()
    	elif choice == "2":
    		PerTbl.addelement()
    	elif choice == "3":
    		running = 0
    	else:
    		rc = 0
    And when I run it, I get this error.
    Code:
    Traceback (most recent call last):
      File "U:/pertbl.py", line 45, in -toplevel-
        PerTbl.addelement()
    TypeError: unbound method addelement() must be called with PerTbl instance as first argument (got nothing instead)
    I don't understand what I'm not doing right.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Very simply, what you are doing is trying to call an instance method without an instance; as a class method. The quickest way to solve this would be to create an instance and call the methods using that.

    If you want to keep things as inline as you can, then you would you change the line PerTbl.addelement() to PerTbl().addelement(). Personally I would probably create a named instance for this.

    You could also create a class method instead of an instance method depending on what version of Python you are running you could use the built-in classmethod() function. Or if you have Python 2.4 you could use the new function decorator syntax. Here's an example:

    Code:
    >>> class A:
    ...     def hello(self):
    ...         print 'hello'
    ...     hello = classmethod(hello)
    ... 
    >>> A.hello()
    hello
    >>> class B:
    ...     @classmethod
    ...     def hello(self):
    ...         print 'hello'
    ... 
    >>> B.hello()
    hello
    >>>
    If you want to learn more about function decorators then so a quick search on google.

    Edit: Python.org is still acting up but here's the link to the section of the docs which covers function decorators.

    http://www.python.org/dev/doc/devel/whatsnew/node6.html

    Hope this helps,

    Mark.
    Last edited by netytan; February 10th, 2005 at 03:42 PM.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    88
    Rep Power
    10
    thankyou for the help.
    But now...I'm having trouble writing the file to where I want, because whenever I put in a '\' into the location where I'm going to save, it makes everything a string, but when I don't do that, like below, it says it can't find the file... help again please.
    Code:
    #Created by Pinko (C) 2005
    #Periodic Python project started Feb. 10, 2005
    #Protected under the GNU Public Liscens
    #Visit me on the web @ http://pinko.zetasystems.com
    import os
    print "Welcome to Periodic Python"
    print "Version 0.01"
    print "~~~~~~~~~~~"
    
    class PerTbl:
    	def addelement(self):
    		element = raw_input("What is the name of this element? ")
    		pernum = raw_input("What is its atomic number? ")
    		atmwei = raw_input("What is its atomic weight? ")
    		prioxinum = raw_input("What is its primary oxidation number? ")
    		symbol = raw_input("What is its symbol?[No Caps] ")
    		sto = open("C:/Program Files/PP/Elements/"+symbol+".txt",'w')
    		sto.write(element+"/n")
    		sto = open("C:/Program Files/PP/Elements/"+symbol+".txt",'a')
    		sto.write(pernum+"/n")
    		sto.write(atmwei+"/n")
    		sto.write(prioxinum+"/n")
    		sto.write(symbol+"/n")
    		sto.close()
    	def searchelement(self):
    		query = raw_input("Please enter the atomic symbol of the element you which to get information on ")
    		search = "/"+query+".txt"
    		dir = os.getcwd()
    		if search in dir:
    			search = open(search,'r')
    			info = search.readlines()
    			print info
    			search.close()
    		else:
    			print "I'm sorry, that element is not in our database currently"
    running = 1
    while running == 1:	
    	rc = 0
    	while rc == 0:
    		print "What would you like to do?"
    		print "1- Get Info on an element"
    		print "2- Add an element"
    		print "3- Exit"
    		choice = raw_input("> ")
    		rc = 1
    	if choice == "1":
    		per = PerTbl()
    		per.searchelement()
    	elif choice == "2":
                    per = PerTbl()
    		per.addelement()
    	elif choice == "3":
    		running = 0
    	else:
    		rc = 0
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    There shouldn't be any difference between using / and \ in your code - you may need to escape the backslashes, either with another backslash or using the raw flag for the sting - however if its working with a backslash why not use that?

    Also, in addelement() you open a file for writing then open the same file in append mode before you close the already open files. This may also be causing you problems. Anyway I would remove the line that opens the file for appending since it's not needed .

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    88
    Rep Power
    10
    Thanks.
    Just one more thing.
    In These lines:
    Code:
    sto = open("C:/Program Files/PP/Elements/"+symbol+".txt",'w')
    		sto.write(element+"/n")
    		sto = open("C:/Program Files/PP/Elements/"+symbol+".txt",'a')
    		sto.write(pernum+"/n")
    		sto.write(atmwei+"/n")
    		sto.write(prioxinum+"/n")
    		sto.write(symbol+"/n")
    		sto.close()
    The program doesn't put the information on the a new line as it should when I add the "/n"

    Also, how do you "escape a backslash", i don't know how...
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    This is because "\n" is a newline character and "/n" is a string containing two separate characters: a forward-slash and an n character. Replace "/n" with "\n" and all should go well.

    From Python 2.3 backslashes are escaped for you, this Is especial useful for paths on Windows. Here's a small section from my Python shell:

    Code:
    >>> 'C:\Python23\python.exe'
    'C:\\Python23\\python.exe'
    >>> r'C:\Python23\python.exe'
    'C:\\Python23\\python.exe'
    >>> 
    >>> 'C:\\Python23\\python.exe'
    'C:\\Python23\\python.exe'
    >>> r'C:\\Python23\\python.exe'
    'C:\\\\Python23\\\\python.exe'
    >>>
    >>>
    >>> '\n'
    '\n'
    >>> '\\n'
    '\\n'
    >>> r'\n'
    '\\n'
    >>>
    This illustrates how to escape characters in Python strings... either by adding a backslash (before another backslash or other special character) or by adding an r before a normal string. The r here causes any special characters to be escaped .

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo