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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:12:33+00:00 2026-06-04T17:12:33+00:00

I am trying to use getattr function in my code using generator li=[] m=[method

  • 0

I am trying to use getattr function in my code using generator

li=[]
m=[method for method in dir(li) if callable(getattr(li,method))]
print getattr(li,(str(i) for i in m))

Error:

TypeError: getattr(): attribute name must be string

if I am using string coercion on i, then why this error shows up?

Also,if I use the code

li=[]
m=[method for method in dir(li) if callable(getattr(li,method))]
for i in range(10):
    print getattr(li,str(m[i]))

Then there is no error

I am new to python, forgive me if i am making very elementary mistake, please can someone elaborate on the error. Thanks

Edit: The same principle works for this code (it is an example from Dive into python). Here,the same thing is done then why there is no error?

def info(object, spacing=10, collapse=1):
    """Print methods and doc strings.

    Takes module, class, list, dictionary, or string."""
    methodList = [e for e in dir(object) if callable(getattr(object, e))]
    processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
    print "\n".join(["%s %s" %
                     (method.ljust(spacing),
                      processFunc(str(getattr(object, method).__doc__)))
                     for method in methodList])
  • 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-04T17:12:34+00:00Added an answer on June 4, 2026 at 5:12 pm

    OK, given your edit I have changed my answer. You appear to be expecting generators to do something different to what they do.

    You don’t pass a generator to a function and have the function work on each item produced by the generator, you loop over a generator and then perform the functions you want inside the loop.

    However, here you don’t need a generator expression – just loop over your list – E.g:

    for method in m:
        print(getattr(li, method))
    

    If you did want to use a generator expression, then you could use it here instead of constructing the list in the first place:

    for method in (method for method in dir(li) if callable(getattr(li, method))):
        print(getattr(li, method))
    

    Although note that, for what you are trying to do here, the inspect module can help avoid a lot of what you are doing.

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

Sidebar

Related Questions

Trying to use this method (gist of which is use self.method_name in the FunnyHelper
Trying to use this code to connect the AD PrincipalContext context = new PrincipalContext(ContextType.Domain,
I have been trying use the edit_post_link() function to contain an image. All of
I am trying use a javascript function while passing php variables in it. For
I am trying use a javascript function while passing php variables in it. For
I'm trying use the Str[fixnum] to return a specific portion of a string. #
I am trying use MySql and Entity Framework, using Connector/Net 6.1 with this as
Anyone know how to create a cvCreateStructuringElementEx using a image? I'm trying use opencv.cv.cvCreateStructuringElementEx()
I'm trying use make a div's background transparent using a mixture of CSS3 rgba()
I am trying use the onClick() function in an activity for an android app.

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.