Python, referencing a sub-class

Posted on 2009-12-29
Last Modified: 2012-05-08
Hello experts,

I am having a problem that I hope there is an solution to.

I have a base class called Document. I then have few sub-classes of Document (Book, Magazine, ...). My problem lies in the fact that a Book has an author attribute but a Magazine doesn't. So when I do:

documents = bookModel.Document.objects.all()

for document in documents:

I get nothing since the base class doesn't have an author attribute.

Do these instanced of a base class not know they are really  implemented as sub-classes ?

I hope I am being clear enough
Question by:mannsi
    LVL 41

    Accepted Solution

    Q: Do base classes "know" that they have been extended (have descendants)?
    A: No.

    You might choose to define an author attribute for documents, and default the value to None until a descendant needs this.  But this isn't normally done.

    You could use something like this though, to see if the object instance has/contains the "author" attribute...

    if 'author' in dir( document ) :
      # do something with the ...

    But that really isn't very object oriented.
    LVL 41

    Expert Comment

    Thanks for the grade & points.

    Good luck & Happy New Year.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
    Article by: Swadhin
    Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now