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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:34:24+00:00 2026-06-12T05:34:24+00:00

An example from the book Core Python Programming on the topic Delegation doesn’t seem

  • 0

An example from the book Core Python Programming on the topic Delegation doesn’t seem to be working.. Or may be I didn’t understand the topic clearly..

Below is the code, in which the class CapOpen wraps a file object and defines a modified behaviour of file when opened in write mode. It should write all strings in UPPERCASE only.

However when I try to open the file for reading, and iterate over it to print each line, I get the following exception:

Traceback (most recent call last):
  File "D:/_Python Practice/Core Python Programming/chapter_13_Classes/
        WrappingFileObject.py", line 29, in <module>
    for each_line in f:
TypeError: 'CapOpen' object is not iterable

This is strange, because although I haven’t explicitly defined iterator methods, I’d expect the calls to be delegated via __getattr__ to the underlying file object. Here’s the code. Have I missed anything?

class CapOpen(object):
    def __init__(self, filename, mode='r', buf=-1):
        self.file = open(filename, mode, buf)

    def __str__(self):
        return str(self.file)

    def __repr__(self):
        return `self.file`

    def write(self, line):
        self.file.write(line.upper())

    def __getattr__(self, attr):
        return getattr(self.file, attr)


f = CapOpen('wrappingfile.txt', 'w')
f.write('delegation example\n')
f.write('faye is good\n')
f.write('at delegating\n')
f.close()

f = CapOpen('wrappingfile.txt', 'r')

for each_line in f:   # I am getting Exception Here..
    print each_line,

I am using Python 2.7.

  • 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-12T05:34:25+00:00Added an answer on June 12, 2026 at 5:34 am

    This is a non-intuitive consequence of a Python implementation decision for new-style classes:

    In addition to bypassing any instance attributes in the interest of
    correctness, implicit special method lookup generally also bypasses
    the __getattribute__() method even of the object’s metaclass…

    Bypassing the __getattribute__() machinery in this fashion provides
    significant scope for speed optimisations within the interpreter, at
    the cost of some flexibility in the handling of special methods (the
    special method must be set on the class object itself in order to be
    consistently invoked by the interpreter).

    This is also explicitly pointed out in the documentation for __getattr__/__getattribute__:

    Note
    This method may still be bypassed when looking up special methods as
    the result of implicit invocation via language syntax or built-in
    functions. See Special method lookup for new-style classes.

    In other words, you can’t rely on __getattr__ to always intercept your method lookups when your attributes are undefined. This is not intuitive, because it is reasonable to expect these implicit lookups to follow the same path as all other clients that access your object. If you call f.__iter__ directly from other code, it will resolve as expected. However, that isn’t the case when called directly from the language.

    The book you quote is pretty old, so the original example probably used old-style classes. If you remove the inheritance from object, your code will work as intended. That being said, you should avoid writing old style classes, since they will become obsolete in Python 3. If you want to, you can still maintain the delegation style here by implementing __iter__ and immediately delegating to the underlying self.file.__iter__.

    Alternatively, inherit from the file object directly and __iter__ will be available by normal lookup, so that will also work.

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

Sidebar

Related Questions

I am trying out an example from the book Foundations of python network programming,
I am trying to get an example from the book Professional Android Programming with
I am working on an example from a php book and am getting an
I'm a newbie to scala. I'm trying an example from the book Programming Scala.
I have read the Core Animation Cook Book and Core Animation Programming Guide from
I want to try a lighting example from the book OpenGL ES 2.0 Programming
I am working on Ajax examples from a book and the example from the
I would like to rewrite the python code example from the book to C#
I opened the core data book example directly from Xcode's documentation window. The project
I'm doing the matrix multiplication example from the book CUDA C Programming Guide, page

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.