I am just starting to look at writing class in Python. Can a class that I write inherit Python calendar?

Recommended Answers

All 3 Replies

I'm not very certain on classes and stuff like that myself but i'm guessing and from what i've read i think if you do

class namehere(calender_name):
    stuff here

can't guarantee it will work (not on my computer so i can't get at python to try it)
but it's just an idea

You can only inherit another class. The module calendar contains a function calendar, but you can't inherit a function. This small code might explain a few basic things ...

import calendar
import Cookie
import inspect

if inspect.isclass(calendar):
    print "calendar is a class"
else:
    print "calendar is not a class"

# this would give you an error since calendar is not a class, but a module
# there is a function (not a class) calendar within module calendar
# you can use functions like calendar.calendar(), but not inherit them
"""
class MyCalendar(calendar):
    pass
"""

if inspect.isclass(Cookie.Morsel):
    print "Cookie.Morsel is a class"
else:
    print "Cookie.Morsel is not a class"

# this will work, since Morsel is a class in module Cookie
class MyCookie(Cookie.Morsel):
    "now do something more than pass"
    pass


print type(calendar)          # <type 'module'>
print type(calendar.calendar) # <type 'function'>

print type(Cookie)            # <type 'module'>
print type(Cookie.Morsel)     # <type 'type'>   hint of class

Note: A class name by convention starts with a capital letter, this can be a hint!

I am astonished how little classes basic Python modules uses. I thought it was more of a OO language.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.