New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improper object initialization #36360
Comments
The descr tutorial specifies this rule: __new__ must return an object. There's nothing that I believe that this rule adds an unnecessary wart to So basically, my argument is for explicit versus Here is an example where the current rule results class Foo(object):
def __new__(cls, x):
if x == 0:
return [1,2,3]
if x == 1:
return 1
if x == 2:
return (1,2,3)
if x == 3:
return '1'
if x == 4:
return {1:1,2:2,3:3}
else:
return super(cls, Foo).__new__(cls)
for i in range(6):
try:
print 'Foo(%d) =' % i,Foo(i)
except:
print 'Foo(%d) failed' % i Which under Python 2.2 results in the following output: Under the proposed new rule, the output would be If there is agreement on this issue, I will submit |
Logged In: YES Patch attached. Passes Python descr tests and our Note: The ugly test to avoid calling tp_init on |
Logged In: YES Thanks, Kevin! Assigned to Guido, under the theory that it |
Logged In: YES I think Kevin's suggestion is about right, but I'd like to It's a clear semantic change, so I don't want to change this |
Logged In: YES Fixed in CVS, using PyType_IsSubtype(obj->type, type). Note that the exception for type(x) is still needed (the |
Logged In: YES Given Kevin's plea, I've backported this to Python 2.2.2b1, |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: