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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:57:55+00:00 2026-05-18T08:57:55+00:00

I recently created a @sequenceable decorator, that can be applied to any function that

  • 0

I recently created a @sequenceable decorator, that can be applied to any function that takes one argument, and causes it to automatically be applicable to any sequence. This is the code (Python 2.5):

def sequenceable(func):
    def newfunc(arg):
        if hasattr(arg, '__iter__'):
            if isinstance(arg, dict):
                return dict((k, func(v)) for k, v in arg.iteritems())
            else:
                return map(func, arg)
        else:
            return func(arg)
    return newfunc

In use:

@sequenceable
def unixtime(dt):
    return int(dt.strftime('%s'))

>>> unixtime(datetime.now())
1291318284
>>> unixtime([datetime.now(), datetime(2010, 12, 3)])
[1291318291, 1291352400]
>>> unixtime({'start': datetime.now(), 'end': datetime(2010, 12, 3)})
{'start': 1291318301, 'end': 1291352400}

My questions are:

  • Is this a terrible idea, and why?
  • Is this a possibly good idea, but has significant drawbacks as implemented?
  • What are the potential pitfalls of
    using this code?
  • Is there a builtin or library that
    already does what I’m doing?
  • 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-18T08:57:56+00:00Added an answer on May 18, 2026 at 8:57 am

    This is a terrible idea. This is essentially loose typing. Duck-typing is as far as this
    stuff should be taken, IMO.

    Consider this:

    def pluralize(f):
        def on_seq(seq):
            return [f(x) for x in seq]
        def on_dict(d):
             return dict((k, f(v)) for k, v in d.iteritems())
        f.on_dict = on_dict
        f.on_seq = on_seq
        return f
    

    Your example then becomes

    @pluralize
    def unixtime(dt):
        return int(dt.strftime('%s'))
    
    
    unixtime.on_seq([datetime.now(), datetime(2010, 12, 3)])
    unixtime.on_dict({'start': datetime.now(), 'end': datetime(2010, 12, 3)})
    

    Doing it this way still requires the caller to know (to within duck-typing accuracy) what is being passed in and doesn’t add any typechecking overhead to the actual function. It will also work with any dict-like object whereas your original solution depends on it being an actual subclass of dict.

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

Sidebar

Related Questions

I recently created a turn-based game server that can accept 10s of thousands of
I recently created a Windows service. One thing that it faithfully does is log
I recently created a site that simply has a variable and checks it against
I recently created an auto browser windows form app that browsers various places just
I recently created a advanced form with elements that use jquery's $().hide & $().show
I recently created a generic Matrix<T> class that acts as a wrapper around a
I recently created a windows service which can wear various hats depending on what
Recently I created a script that accesses another script to run it. The only
I recently created an xml feed -> html javascript function for an iPhone app
I've recently created these two (unrelated) methods to replace lots of boiler-plate code in

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.