-4

Below is my code . I am unable to call functons of google_charts class in my Demo class. Help me fix the problem.

class google_charts:
    def all_files_to_one():
        pass

    prods = []
    srcs = []
    sname = []
    def process_compiled():
        global prods, srcs, sname
        sname.append('something')
        prods.append('something')
        srcs.append('something')

def demo():
    google_charts.all_files_to_one()
    google_charts.process_compiled()

demo()

The output:

Traceback (most recent call last):
  File "C:\\demo.py", line 18, in <module>
    demo()
  File "C:\\demo.py", line 15, in demo
    google_charts.all_files_to_one()
TypeError: unbound method all_files_to_one() must be called with google_charts instance as first argument (got nothing instead)
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251

4 Answers4

4

When defining a class in python, any methods within it must have 'self' as the first agrument:

class cheese(object):
       def some_method(self):
           return None
Infiltrator
  • 1,611
  • 1
  • 16
  • 25
2

The signature of all methods in your google_contacts class is incorrect. They must include the self parameter. Have a look at the documentation of Python classes:

http://docs.python.org/tutorial/classes.html

This may also be useful:

What is the purpose of self?

Community
  • 1
  • 1
sebastian
  • 4,914
  • 2
  • 21
  • 21
1

You want to use:

self.all_files_to_one()

Apart from that: the 'self' parameter is missing

This question is truly basic Python object-oriented programming.

Please read a Python tutorial first instead playing trial-and-error programming.

SO is not a replacement for reading basic documentation.

1

I took the liberty of editing the original question removing the long, irrelevant code to make the actual problem visible. It looks like you've taken a bunch of functions and just wrapped a class around them. In Python class methods need a self parameter. The original functions were all of style all_files_to_one which had no state for a class to operate on. One function did (process_compiled). Here's a corrected version:

class google_charts(object):  # Python 2.X classes should derive from object

    # Initialize class instance state variables
    def __init__(self):
        self.prods = []  
        self.srcs = []
        self.sname = []

    # methods are called with the class instance object, traditionally named self
    def process_compiled(self):
        # Access instance variables via self
        self.sname.append('something')
        self.prods.append('something')
        self.srcs.append('something')

    # Use a static method when a method does not access class or instance state
    # but logically is associated with the class.  No self parameter is passed
    # for a static method.
    @staticmethod
    def all_files_to_one():
        print 'called all_files_to_one()'

def demo():
    gc = google_charts()  # instantiate an object
    gc.all_files_to_one() # call static method
    gc.process_compiled() # call instance method
    print gc.prods        # see some state in the instance

demo()

Output:

called all_files_to_one()
['something']

Please read a tutorial as the other posters have mentioned.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251