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

  • Home
  • SEARCH
  • 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 248319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:19:59+00:00 2026-05-11T21:19:59+00:00

e.g. so that these would both work – is it possible? (val,VAL2) = func(args)

  • 0

e.g. so that these would both work – is it possible?

(val,VAL2) = func(args) 
val = func(args)

Where val is not a tuple

For example I’d like these to work for my custom object something

for item in something:
    do_item(item) #where again item - is not a tuple

for (item,key) in something:
    do_more(key,item)

I thought that I need to implement next() function in two different ways…

edit: as follows from the answers below, this should not really be done.

  • 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-11T21:19:59+00:00Added an answer on May 11, 2026 at 9:19 pm

    If you mean, can the function act differently based on the return types the caller is expecting, the answer is no (bar seriously nasty bytecode inspection). In this case, you should provide two different iterators on your object, and write something like:

    for item in something:  # Default iterator: returns non-tuple objects
        do_something(item)
    
    for (item,key) in something.iter_pairs(): # iter_pairs returns different iterator
        do_something_else(item, key)
    

    eg. see the dictionary object, which uses this pattern. for key in mydict iterates over the dictionary keys. for k,v in mydict.iteritems() iterates over (key, value) pairs.

    [Edit] Just in case anyone wants to see what I mean by “seriously nasty bytecode inspection”, here’s a quick implementation:

    import inspect, opcode
    
    def num_expected_results():
        """Return the number of items the caller is expecting in a tuple.
    
        Returns None if a single value is expected, rather than a tuple.
        """
        f = inspect.currentframe(2)
        code = map(ord, f.f_code.co_code)
        pos = f.f_lasti
        if code[pos] == opcode.opmap['GET_ITER']: pos += 1 # Skip this and the FOR_ITER
        if code[pos] > opcode.EXTENDED_ARG: pos +=5
        elif code[pos] > opcode.HAVE_ARGUMENT: pos +=3
        else: pos += 1
        if code[pos] == opcode.opmap['UNPACK_SEQUENCE']:
            return code[pos+1] + (code[pos+2] << 8)
        return None
    

    Usable something like:

    class MagicDict(dict):
        def __iter__(self):
            if num_expected_results() == 2:
                for k,v in self.iteritems():
                    yield k,v
            else:
                for k in self.iterkeys(): 
                    yield k
    
    d=MagicDict(foo=1, bar=2)
    
    print "Keys:"
    for key in d:
        print "   ", key
    print "Values"    
    for k,v in d:
        print "   ",k,v
    

    Disclaimer: This is incredibly hacky, insanely bad practice, and will cause other programmers to hunt you down and kill you if they ever see it in real code. Only works on cpython (if that). Never use this in production code (or for that matter, probably any code).

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

Sidebar

Ask A Question

Stats

  • Questions 211k
  • Answers 211k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This helped me understand the reasoning: The main difference is,… May 12, 2026 at 10:15 pm
  • Editorial Team
    Editorial Team added an answer NOT EXISTS is generally better (although in your case if… May 12, 2026 at 10:15 pm
  • Editorial Team
    Editorial Team added an answer This was really what i was looking for... iReportPath TextReader… May 12, 2026 at 10:15 pm

Related Questions

e.g. so that these would both work - is it possible? (val,VAL2) = func(args)
In an existing application, I have a table which has no primary key, which
I would like to provide all mathematical functions for the number-like objects created by
I have a table which maintains performance data of a system, each record is
I have a large number of text files (1000+) each containing an article from

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.