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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:00:34+00:00 2026-05-28T14:00:34+00:00

The operator.itemgetter() function works like this: >>> import operator >>> getseconditem = operator.itemgetter(1) >>>

  • 0

The operator.itemgetter() function works like this:

>>> import operator
>>> getseconditem = operator.itemgetter(1)
>>> ls = ['a', 'b', 'c', 'd']
>>> getseconditem(ls) 
'b'

EDIT I’ve added this portion to highlight the inconsitency

>>> def myitemgetter(item):
...     def g(obj):
...         return obj[item]
...     return g
>>> mygetseconditem = myitemgetter(1)

Now, I have this class

>>> class Items(object):
...     second = getseconditem
...     mysecond = mygetseconditem
...
...     def __init__(self, *items):
...         self.items = items
...
...     def __getitem__(self, i):
...         return self.items[i]

Accessing the second item with its index works

>>> obj = Items('a', 'b', 'c', 'd')
>>> obj[1] 
>>> 'b'

And so does accessing it via the mysecond method

>>> obj.mysecond()
'b'

But for some reason, using the second() method raises an exception

>>> obj.second()
TypeError: itemgetter expected 1 arguments, got 0

What gives?

  • 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-28T14:00:35+00:00Added an answer on May 28, 2026 at 2:00 pm

    obj.second is the function getseconditem. A function which expects an argument to operate on. Since you call obj.second without any arguments the error you gave is raised. To solve it you can do obj.second(obj.items) or define second differently:

    class Items(object):
        def __init__(self, *items):
            self.items = items
    
        def __getitem__(self, i):
            return self.items[i]
    
        def second(self):
            return getseconditem(self.items)
    

    Edit

    It’s clear what you mean now after you edited your question. I think what’s going on here is that because getseconditem is not a user-defined function it does not get transformed to a method upon accessing obj.second. It simply stays a function. The following can be found in the docs:

    Note that the transformation from function object to (unbound or
    bound) method object happens each time the attribute is retrieved from
    the class or instance. In some cases, a fruitful optimization is to
    assign the attribute to a local variable and call that local variable.
    Also notice that this transformation only happens for user-defined
    functions; other callable objects (and all non-callable objects) are
    retrieved without transformation.

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

Sidebar

Related Questions

I start out with date strings: from operator import itemgetter import datetime as DT
LIKE operator in MySql is used to find rows that contain our query text,
To illustrate, I start with a list of 2-tuples: import itertools import operator raw
operator class: #import <Foundation/Foundation.h> @interface operator : NSObject { int number; } @property int
What is the operator int function below? What does it do? class INT {
I'm creating a generator that gets consumed by another function, but I'd still like
You can overload operator true and false i looked at examples and found this
why The assignment operator must be a non-static member function operator(), operator[], and operator->
Anyone knows if multiply operator is faster than using the Math.Pow method? Like: n
is there a JavaScript operator that will check proximity, kind of like a soft

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.