Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 17th, 2006, 03:55 PM
rzed
Guest
 
Posts: n/a
Default Class and instance question

I'm confused (not for the first time).

I create these classes:

class T(object):
def __new__(self):
self.a = 1

class X(T):
def __init__(self):
self.a = 4

class Y:
def __init__(self):
self.a = 4

class Z(object):
def __init__(self):
self.a = 4


.... and these instances:

t = T()
x = X()
y = Y()
z = Z()

and I want to examine the 'a' attributes.
Quote:
Quote:
Quote:
>>print T.a
1
Quote:
Quote:
Quote:
>>print y.a
4
Quote:
Quote:
Quote:
>>print z.a
4

So far, it's as I expect, but:
Quote:
Quote:
Quote:
>>print x.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'a'
Quote:
Quote:
Quote:
>>print t.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'a'

So what the heck is 'T'? It seems that I can't instantiate it or
derive from it, so I guess it isn't a proper class. But it's
something; it has an attribute. What is it? How would it be used
(or, I guess, how should the __new__() method be used)? Any hints?

--
rzed
  #2  
Old December 17th, 2006, 04:25 PM
Marco Wahl
Guest
 
Posts: n/a
Default Re: Class and instance question

rzed <rzantow@gmail.comwrites:

To simplify take
Quote:
class T(object):
def __new__(self):
self.a = 1
and

t = T()

and then you get
Quote:
Quote:
Quote:
>>print t.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'a'
While T.a is 1.
Quote:
So what the heck is 'T'? It seems that I can't instantiate it or
derive from it, so I guess it isn't a proper class. But it's
something; it has an attribute. What is it?
I don't know.
Quote:
How would it be used
(or, I guess, how should the __new__() method be used)? Any hints?
The __new__ method should return the class. In your
case return is None. Further the parametername for the
__new__ method should be better cls to have a
distinction to the usual self for instances.

See http://www.python.org/doc/2.4.4/ref/customization.html


Best wishes

  #3  
Old December 17th, 2006, 05:05 PM
Colin J. Williams
Guest
 
Posts: n/a
Default Re: Class and instance question

rzed wrote:
Quote:
I'm confused (not for the first time).
>
I create these classes:
>
class T(object):
def __new__(self):
self.a = 1
>
class X(T):
def __init__(self):
self.a = 4
>
class Y:
def __init__(self):
self.a = 4
>
class Z(object):
def __init__(self):
self.a = 4
>
>
... and these instances:
>
t = T()
x = X()
y = Y()
z = Z()
>
and I want to examine the 'a' attributes.
>
Quote:
Quote:
>>>print T.a
1
Quote:
Quote:
>>>print y.a
4
Quote:
Quote:
>>>print z.a
4
>
So far, it's as I expect, but:
>
Quote:
Quote:
>>>print x.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'a'
>
Quote:
Quote:
>>>print t.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'a'
>
So what the heck is 'T'? It seems that I can't instantiate it or
derive from it, so I guess it isn't a proper class. But it's
something; it has an attribute. What is it? How would it be used
(or, I guess, how should the __new__() method be used)? Any hints?
>
__new__ should return something. Since there is no return statement,
None is returned.

You might try something like:

class T(object):
def __new__(cls):
cls= object.__new__(cls)
cls.a= 1
return cls
t= T()
print t.a

Colin W.

  #4  
Old December 18th, 2006, 02:45 AM
Scott David Daniels
Guest
 
Posts: n/a
Default Re: Class and instance question

Colin J. Williams wrote:
Quote:
rzed wrote:
Quote:
>class T(object):
> def __new__(self):
> self.a = 1
>...
>t = T()
>>
>and I want to examine the 'a' attributes.
>>
Quote:
>>>>print T.a
>1
Quote:
>>>>print t.a
>Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
>AttributeError: 'NoneType' object has no attribute 'a'
>>
>So what the heck is 'T'? It seems that I can't instantiate it or
>derive from it, so I guess it isn't a proper class. But it's
>something; it has an attribute. What is it? How would it be used (or,
>I guess, how should the __new__() method be used)? Any hints?
>>
__new__ should return something. Since there is no return statement,
None is returned.
>
You might try something like:
>
class T(object):
def __new__(cls):
cls= object.__new__(cls)
cls.a= 1
return cls
t= T()
print t.a
Or, to use a bit more revealing names:
class NewT(object):
def __new__(class_):
instance = object.__new__(class_)
instance.a = 1
return instance

You might have figured more of this out with:
Quote:
Quote:
Quote:
>>t = T()
>>print repr(t)
>>newt = NewT()
>>print repr(newt)
>>T.a
>>t.a
--Scott David Daniels
scott.daniels@acm.org
  #5  
Old December 18th, 2006, 10:55 AM
Bruno Desthuilliers
Guest
 
Posts: n/a
Default Re: Class and instance question

Marco Wahl a écrit :
(snip)
Quote:
The __new__ method should return the class.
s/class/instance/
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles