-1

I'm trying to inherit from this class:

class Event(Clock, Calendar):

    def __init__(self):
        year,month,day, hours, minutes,seconds = time.localtime()[0:6]

        eClock = Clock(hours,minutes,0)
        eCal = Calendar(month, day, year)

    def createEvent(self,year,month,day,hours,minutes):
        year,month,day = date[0:]
        hours,minutes = ttime[0:2]

In order to create an event here:

sett = line[1:].split(",") # Line[1:] is going to be a
                           # date, such as 1/8/17 17:50.
date = sett[0]
ttime = sett[1]
ttime = ttime.split(":")
date = date.split("/")
Cevent = ttime + date
Cevent.event()

I have another class, called Reminder, that inits this:

event = Event.createEvent()

Anytime I try to run this program though, it gives me this error:

TypeError: unbound method createEvent() must be called with Event instance as first argument (got nothing instead)

Im wondering why, and how I could take the method createEvent and use it in another class in the same file.

MSeifert
  • 145,886
  • 38
  • 333
  • 352
  • I actually don't know what you're trying to achieve here. The error is because `createEvent` is a normal method, make it a `classmethod` or `staticmethod` or call it on an actual instance `Event().createEvent()`. But I really don't know what your class is trying to do because nothing is saved as properties and nothing is returned. – MSeifert Dec 13 '16 at 03:08
  • If `createEvent` is not intended to be an instance attribute, you could just as easily define it at the module level - it doesn't have to be in a class. If you want it to be a class method instead of an instance method decorate/define it like this -https://docs.python.org/3/library/functions.html#classmethod. For a static method - https://docs.python.org/3/library/functions.html#staticmethod. – wwii Dec 13 '16 at 04:42
  • http://stackoverflow.com/a/27568860/2823755 - You should look at all the answers in the Q&A. – wwii Dec 13 '16 at 04:44

1 Answers1

0

A bound method means that the method is called from a class. Let's look at an example:

class MyClass:
    def add(self, x, y):
        return x+y

vs

def add_numbers(x, y):
    return x+y

The method add_numbers() is an unbound method, meaning it is not attached to any class instance. To use it, we can just call:

print(add_numbers(1, 2))

However, when we want to call the method add(), we need an instance of the class MyClass:

class_instance = MyClass()
print(class_instance.add(1, 2))

Notice that when we want to call the add() method, we first have to create a new instance of the class, then use that to call the method, Under the hood, python takes the class_instance variable and passes it to the method as the 'self' argument seen in the function definition.

In closing, your issue is in the line:

event = Event.createEvent()

The error is telling you that the method is expecting an instance of an event class, and not the class itself. If the Event class can be instantiated without arguments, then the correct syntax would be:

base_event = Event()
event = base_event.createEvent()

Of course, the method of instantiating the base_event variable will depend on the API you're trying to use.

Alex Barry
  • 415
  • 1
  • 9
  • 21