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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:41:11+00:00 2026-06-12T22:41:11+00:00

Python 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3] on linux2 Type help, copyright,

  • 0
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Image
>>> im = Image.open("test.jpeg")
>>> data = im.load()
>>> data.__setitem__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'PixelAccess' object has no attribute '__setitem__'
>>> help(data)

>>> data.__setitem__
<method-wrapper '__setitem__' of PixelAccess object at 0x7f4d9ae4b170>

This is the strangest thing I’ve ever seen.

I’m doing a project with library PIL. ‘data‘ is an object of PixelAccess. It has the attribute of __setitem__ in help(data).

You can do ‘data[x,y] = value‘ to assign the pixel value in coordinate (x,y)

Help on PixelAccess object:

class PixelAccess(object)
 |  Methods defined here:
 |  
 |  __delitem__(...)
 |      x.__delitem__(y) <==> del x[y]
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __setitem__(...)
 |      x.__setitem__(i, y) <==> x[i]=y

Why doesn’t __setitem__ exist before help() function but appear after it?

It is the same even after I execute the express ‘data[x,y] = value‘. It only appears after help() function.

How to explain it?

  • 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-12T22:41:12+00:00Added an answer on June 12, 2026 at 10:41 pm

    Indeed – it is somestrange behavior.
    But the problem is not that __getitem__ does not exist – it is there, just temporarily hidden due to some issue in PIL code – probably a bug resulting from the mechanisms that allow lazy loading an image.

    This should not trouble you – the “data” in your case is a PIL’s “PixelAccess” object, which allows you to access your image pixels using tuples as indexes. It does show “empty” when performing a “dir” on it, and will raise an attribute error if you try to get its __getitem__ method, indeed:

    >>> from PIL import Image
    >>> img = Image.open("images/rock.png")
    >>> d1 = img.load()
    >>> dir (d1)
    []
    >>> d1.__getitem__
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'PixelAccess' object has no attribute '__getitem__'
    >>> # However, this works:
    ... 
    >>> d1[32,32]
    (255, 255, 255, 255)
    >>> d1.__getitem__
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'PixelAccess' object has no attribute '__getitem__'
    >>> 
    

    as I said, thats ir likely a side effect of whatever mechanisms PIL uses
    to create the PixelAcces object.
    If you really need access to your PixelAccess.__getitem__ method, the way to get it
    is the way to bypass most attribute hidding tricks in Python: you do use
    the object class __getattribute__ method to retrieve it.

    (And once it is done this way, surprise, the PixelAccess object no longer appears empty to dir):

    >>> dir(d1)
    []
    >>> getattr(d1, "__getitem__")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'PixelAccess' object has no attribute '__getitem__'
    >>> object.__getattribute__(d1, "__getitem__")
    <method-wrapper '__getitem__' of PixelAccess object at 0x7f94e6f37170>
    >>> # there is our man
    ... 
    >>> getattr(d1, "__getitem__")
    <method-wrapper '__getitem__' of PixelAccess object at 0x7f94e6f37170>
    >>> # and this works now
    ... 
    >>> dir(d1)
    ['__class__', '__delattr__', '__delitem__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__']
    >>> 
    

    All in all, the object.__getattribute__ call as shown above should be “deterministic” for you to fetch the method.

    As for what mechanisms it is hidden prior to that, and what causes it, and all other members of the object, to show up after introspected in this form, the only way to know is digging into PIL’s code.

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

Sidebar

Related Questions

Python 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3] on linux2 Type help, copyright,
I use Python 2.6 more than I use Leopard's default python installation, so I
I am using python default development http server, and it works fine. Error ,The
Do the following on the default Python install on Mac OS X 10.5 (Leopard)
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm finding that dictionary
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm trying to create
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I was working on
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument def f(a, L=[]): L.append(a)
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32.
By default, the Requests python library writes log messages to the console, along the

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.