mr_bulrathi
mr_bulrathi

Reputation: 554

Use calendar module function

I need to use one of the calendar module functions, but python renders weird output.

from calendar import Calendar as cal
def calend(year):
    print cal.yeardatescalendar(year, width=3)
cal(2015)
>>> TypeError: unbound method yeardatescalendar() must be called with Calendar instance as first argument (got int instance instead)

Ok, lets try

from calendar import Calendar as cal
def calend(year):
    y = cal(2015)
    print cal.yeardatescalendar(y, width=3)
cal(2015)
>>> TypeError: yeardatescalendar() takes at least 2 arguments (2 given)

What am i doing wrong? P.S. Documentation for the module seems to be incomplete.

Upvotes: 0

Views: 476

Answers (1)

snakecharmerb
snakecharmerb

Reputation: 55589

yeardatescalendar is an instance method of the Calendar class, so to call it you need to first create an instance of Calendar class, then call the method on the instance, like this:

import Calendar
def calend(year):                       
    mycalendar = calendar.Calendar()
    print mycalendar.yeardatescalendar(year, width=3) 

If you call the method on the class without first creating an instance then you get the UnboundLocalError like in your first example.

Calendar.yeardatescalendar takes an integer as it's first parameter - in your second example you passed it a Calendar instance.

Upvotes: 1

Related Questions