472,960 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,960 software developers and data experts.

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.
>>print T.a
1
>>print y.a
4
>>print z.a
4

So far, it's as I expect, but:
>>print x.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'a'
>>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
Dec 17 '06 #1
4 993
rzed <rz*****@gmail.comwrites:

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

t = T()

and then you get
>>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.
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.
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

Dec 17 '06 #2
rzed wrote:
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.
>>>print T.a
1
>>>print y.a
4
>>>print z.a
4

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

Dec 17 '06 #3
Colin J. Williams wrote:
rzed wrote:
>class T(object):
def __new__(self):
self.a = 1
...
t = T()

and I want to examine the 'a' attributes.
>>>>print T.a
1
>>>>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:
>>t = T()
print repr(t)
newt = NewT()
print repr(newt)
T.a
t.a
--Scott David Daniels
sc***********@acm.org
Dec 18 '06 #4
Marco Wahl a écrit :
(snip)
The __new__ method should return the class.
s/class/instance/
Dec 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Murat Tasan | last post by:
hi, i am having a small problem with constructing an inner class. i am using an inner class (and not a static nested class) because the methods of the inner class need access to the enclosing...
7
by: Rim | last post by:
Hi, It appears to me the simplest way to add a function to a class outside of the class declaration is as follows: >>> class a(object): .... pass .... >>> def f(self): .... print 'hi'
7
by: Kerry Neilson | last post by:
Hi, Really hung up on this one. I'm trying to get all the fields of a dictionary to be unique for each class: class A { my_dict = dict_entry = { 'key1':0, 'key2':0 } __init__(self): for...
8
by: stoptv | last post by:
Hello group, this is my dilemma: ------------------------------------------------------------------------ #include <iostream> using namespace std; // a regular manipulator ostream & hello(...
1
by: Scott Jacobsen | last post by:
I have a question about the lifecycle of an asp.net class. I have some java background, so here's my question: When I create a code behind class how are instance of that class created/used, and...
2
by: Chris Puncher | last post by:
Hi. I have a RCW class that was generated by the VS.NET2003 IDE when I added a COM dll reference to my project. This all works fine when I call methods on it. My initial problem is that in...
4
by: Tony Johansson | last post by:
Hello! I have a class definition called MyClass see below. I create an instance of this class MyClass I also want this instance to be able to modify the test instance that exist in this...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
5
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject();...
4
by: Bit Byter | last post by:
I want to write a (singleton) container for instances of my class templates, however, I am not too sure on how to: 1). Store the instances 2). How to write the acccesor method (instance()) to...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.