Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1021311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:17:33+00:00 2026-05-16T11:17:33+00:00

OK, so I’m writing a framework that looks for python files in sub directories

  • 0

OK, so I’m writing a framework that looks for python files in sub directories that are named task.py and then look for classes that are derived from the base class Task and collect them.
I decided that I needed to add a meta class to Task, but then the issubclass() started to behave in a weird way.
Here is how the directory layout looks:

start.py                
tasks/__init__.py
tasks/base.py
tasks/sub/__init__.py   # empty
tasks/sub/task.py

start.py:

#!/usr/bin/env python
from tasks.base import Task1, Task2
from tasks.sub.task import SubTask1, SubTask2

print "Normal import:"
print "is SubTask1 sub class of Task1? %s" % issubclass(SubTask1, Task1)
print "is SubTask2 sub class of Task2? %s" % issubclass(SubTask2, Task2)

from tasks import setup
print "Imp import:"
setup()

tasks/init.py

import os.path, imp, types
from tasks.base import Task1, Task2

# Find all task definitions
ALL_TASK1 = { }
ALL_TASK2 = { }
def _append_class(d, task, base):
    if (type(task) == types.TypeType) and issubclass(task, base):
        if task == base:
            return
        if not task.name in d:
            d[task.name] = task

this_dir = os.path.dirname(os.path.abspath(__file__))
for root, dirs, files in os.walk(this_dir):
    if not "task.py" in files:
        continue
    mod_name = "task"
    f, pathname, description = imp.find_module(mod_name, [ root ])
    m = imp.load_module(mod_name, f, pathname, description)
    f.close()

    for task in m.__dict__.itervalues():
        _append_class(ALL_TASK1, task, Task1)
        _append_class(ALL_TASK2, task, Task2)

def setup():
    print "All Task1: %s" % ALL_TASK1
    print "All Task2: %s" % ALL_TASK2

tasks/base.py

class MetaClass (type):
    def __init__(cls, name, bases, attrs):
        pass

class Base (object): 
    __metaclass__ = MetaClass

    def method_using_metaclass_stuff(self):
        pass

class Task1 (Base):
    pass

class Task2 (object):
    pass 

tasks/sub/task.py

from tasks.base import Task1, Task2

class SubTask1 (Task1): # Derived from the __metaclass__ class
    name = "subtask1"

class SubTask2 (Task2):
    name = "subtask2"

When I run setup.py I got the following output (ALL_TASK1 dict is empty!):

Normal import:
is SubTask1 sub class of Task1? True
is SubTask2 sub class of Task2? True
Imp import:
All Task1: {}
All Task2: {'subtask2': <class 'task.SubTask2'>}

But when I comment out the __metaclass__ line in the class Base (Task1‘s base class) I got the output I expected (ALL_TASK1 dict isn’t empty):

Normal import:
is SubTask1 sub class of Task1? True
is SubTask2 sub class of Task2? True
Imp import:
All Task1: {'subtask1': <class 'task.SubTask1'>}
All Task2: {'subtask2': <class 'task.SubTask2'>}

I don’t understand how the meta class can affect issubclass() when the module is imported via the imp functions, but not when the module is imported with the normal import.

Can someone explain this to me (I’m using python 2.6.1)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:17:34+00:00Added an answer on May 16, 2026 at 11:17 am

    Your code doesn’t work via normal import either.

    >>> from tasks.base import Task1
    >>> type(Task1)
    <class 'tasks.base.MetaClass'>
    >>> from types import TypeType
    >>> type(Task1) == TypeType
    False
    >>> issubclass(type(Task1), TypeType)
    True
    >>> 
    

    When you instantiate the metaclass (as a class), the instance doesn’t have type TypeType, it has type tasks.base.MetaClass. if you test for that in addition to TypeType, then your code works as expected.

    def _append_class(d, task, base):
        if (issubclass(type(task), types.TypeType) and issubclass(task, base):
    

    This yields the same output as when you commented out the metaclass line and has an advantage over your origninal code in that it allows your users to define their own metaclasses and have them function as tasks. If you want to explicitly dissallow this (and please don’t, I might want to use your framework someday) you can just check for both your metaclass or TypeType specifically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I need a function that will clean a strings' special characters. I do NOT
I am writing an app with both english and french support. The app requests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.