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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:39:56+00:00 2026-06-05T16:39:56+00:00

This code produces a different output in Python 2 and Python 3 . class

  • 0

This code produces a different output in Python 2 and Python 3.

class Descriptor(object):
    def __get__(self, instance, owner):
        print('read')
        return 1

    def __set__(self, instance, value):
        print('write')

    def __delete__(self, instance):
        print('del')

class C():
    a = Descriptor()

c = C()                                
c.a                                    
c.a = 3
del c.a
c.a

print('finished')

The output for Python 2 is:

read
read
finished

For Python 3 it is:

read
write
del
read
finished

Why is this working this way? How are Python 2 descriptors different from Python 3 descriptors?

This makes also no sense, because http://docs.python.org/release/3.0.1/reference/datamodel.html#invoking-descriptors clearly describes exactly the same as
http://docs.python.org/reference/datamodel.html#invoking-descriptors

(These are the documentations for Python 2.7 and Python 3.0.)

  • 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-05T16:39:58+00:00Added an answer on June 5, 2026 at 4:39 pm

    Edit: As Ned Deily accurately points out in the comments, the reason this happens is your class C is an old-style class on Python 2, since you haven’t specified object or another new-style class as its base class.


    Because on Python 2, you’re creating a new instance attribute c.a when you do c.a = 3 which hides the descriptor object located at C.a.

    c = C()
    c.a
    c.a = 3
    print c.__dict__['a']
    print C.__dict__['a']
    del c.a
    c.a
    

    gives:

    read
    3
    <__main__.Descriptor object at 0x04625570>
    read
    finished
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This python code works properly and produces proper output: def fib(x): v = 1
This code produces the result as 56. function x ($y) { function y ($z)
This code produces a mess... What am I doing wrong? cell=$(<td>); if(normal.exam_type==Exam_Boolean) { var
library(ggplot2) This code produces a nice looking plot: qplot(cty, hwy, data = mpg, colour
The following code produces the result I don't understand this. Any ideas why? If
This code: System.out.println(String.format(%f, Math.PI)); System.out.println(Math.PI); produces that result on my computer: 3,141593 3.141592653589793 Why
This code, when compiled in xlC 8.0 (on AIX 5.3), produces the wrong result.
The code below produces a NumberFormatException in this line: val cache = cf.createCache(Collections.emptyMap()) Do
I've seen the code examples on this article , but throwing Controller_Exception_404 produces an
Trying to change the __unicode__ method on an instance after it's created produces different

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.