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 9283643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:40:59+00:00 2026-06-18T18:40:59+00:00

I have a function in a Python class that adds Interfaces to a list.

  • 0

I have a function in a Python class that adds Interfaces to a list.

def RegisterAsListener(self, inListener):
    self.__TransitListeners.append(inListener)

This is nice, because a class just needs to inherit from said my Interface, grab this object, and register itself for all updates.

class ITransit():
    def TransitUpdate(self, data):
        raise NotImplementedError("You must define this function.")

(assuming I made an interface correctly)

Since i’m not the only one on this project, I don’t want somebody calling the RegisterAsListener function with an incorrect data type. I could put in code to check the type within the register function, but it would be easier all around if the compiler just yelled at the programmer when they try to shove in an incorrect data type.

def RegisterAsListener(self, IListener inListener):
    self.__TransitListeners.append(inListener)

Is there any way to do this?

  • 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-06-18T18:41:00+00:00Added an answer on June 18, 2026 at 6:41 pm

    Although I would strongly recommend not doing this and only enforcing the implementation of certain methods using Abstract Base Classes (http://docs.python.org/2/library/abc.html), it is possible.

    Here’s an example on how to do something like that: http://www.artima.com/weblogs/viewpost.jsp?thread=101605

    # mm.py
    registry = {}                                                                   
    
    class MultiMethod(object):                                                      
        def __init__(self, name):                                                   
            self.name = name                                                        
            self.typemap = {}                                                       
        def __call__(self, *args):                                                  
            types = tuple(arg.__class__ for arg in args) # a generator expression!  
            function = self.typemap.get(types)                                      
            if function is None:                                                    
                raise TypeError("no match")                                         
            return function(*args)                                                  
        def register(self, types, function):                                        
            if types in self.typemap:                                               
                raise TypeError("duplicate registration")                           
            self.typemap[types] = function                                          
    
    def multimethod(*types):                                                        
        def register(function):                                                     
            function = getattr(function, "__lastreg__", function)                   
            name = function.__name__                                                
            mm = registry.get(name)                                                 
            if mm is None:                                                          
                mm = registry[name] = MultiMethod(name)                             
            mm.register(types, function)                                            
            mm.__lastreg__ = function                                               
            return mm                                                               
        return register                                                             
    
        if hasattr(function, "__lastreg__"):                                        
            function = function.__lastreg__
    

    And the code using it:

    import mm                                                                       
    
    @mm.multimethod(int)                                                            
    def spam(a):                                                                    
        print 'Calling the int method'                                              
        print '%s: %r' % (type(a), a)                                               
    
    @mm.multimethod(float)                                                          
    def spam(a):                                                                    
        print 'Calling the float method'                                            
        print '%s: %r' % (type(a), a)                                               
    
    spam(5)                                                                         
    spam(5.0)
    

    Example output:

    Calling the int method
    <type 'int'>: 5
    Calling the float method
    <type 'float'>: 5.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an uniform list of objects in python: class myClass(object): def __init__(self, attr):
I have this function in Python: def Rotate_Vector(vector, axis, direction): where vector is a
I have a function that its job is generate a python class implicitly according
I have a function within a Python (2.7) class that should retrieve the values
I have a python function that makes a subprocess call to a shell script
I have a python function that randomize a dictionary representing a position specific scoring
I am working with datetime objects in python. I have a function that takes
In python I have the following function: def is_a_nice_element(element, parameter): #do something return True
I have a third-party GUI program that I'm wrapping with a Python class (using
I have a class that handles command line arguments in my program using python's

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.