| New Reply |
Python: Old-style to New-style Class Conversion Causes Error |
Share Thread | Thread Tools |
| Dec11-12, 05:39 PM | #1 |
|
|
Python: Old-style to New-style Class Conversion Causes Error
I'm trying to convert my __init__ to __new__, but I can't find any docs on how the syntax differs or anything really, that pertains to how this bug could be caused by it. What am I doing wrong here?
Code:
import copy;
class grid(list):
def __new__(self, size):
self.size = size;
for i in range(size):
temp = list();
for j in range(size):
temp.append(False);
self.append(temp);
# Other methods that aren't being used and aren't in the trace.
test = grid(2);
Tyler |
| Dec11-12, 06:34 PM | #2 |
|
Mentor
|
Initializing a new member of a class is a job of the __init__ method. Note the final paragraph on __new__: __new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation.You normally don't override __new__. |
| Dec11-12, 06:46 PM | #3 |
|
|
Why would you change __init__() to __new__(). Bad idea! They are not the same and init is the way to go! __new__() is an ugly hack to make inheritance from immutable types work.
Normal inheritance works like this: Code:
class a:
def __init__(self,par):
self.foo=par
def somefunction(self):
print "wakka wakka "+str(self.foo)
class b(a):
def __init__(self,par1,par2):
self.foo=par1
self.bar=par2
def someotherfunction(self):
self.somefunction()
print "wikki wikki "+str(self.bar)
Code:
>>> x=b("gaga","haha")
>>> x.someotherfunction()
wakka wakka gaga
wikki wikki haha
>>> x.somefunction()
wakka wakka gaga
Code:
>>> class b(a):
... def __init__(self,par1,par2):
... b.instance_counter+=1
... self.foo=par1
... self.bar=par2
...
>>> b.instance_counter=0
>>>
>>> e=b("","")
>>> f=b("","")
>>> g=b("","")
>>> print b.instance_counter
3
So what happens when you create a class instance, is that python builds some empty object with the respective member functions, and hands it to the __init__() function, and then it can get modified by init. With immutable types __init__() cannot do its job. After python has created the immutable object, init functions cannot modify it anymore. So __new__() was invented which steps in before init. And it does not receive a pre build self object but a class! So when you use __new__(), you call __new__() of the immutable type that you want to inherit from, giving it the unchangeable value that you want. When you are done creating that object __init__ gets called with the created object as self. Let's go through your code: Code:
import copy;#you don't use copy anywhere in your example
class grid(list):
def __new__(self, size): #self points to the class grid now not to any object
self.size = size; #add a size variable to grid class declaration just like instance_number
for i in range(size):
temp = list(); # Aaaargh why do you use semicolons this is not c!
for j in range(size):
temp.append(False); #fill the list with False
self.append(temp); #call the function grid.append() I don't even know what it is
# it is not an object of grid but the of the grid declaration itself!
|
| Dec11-12, 09:19 PM | #4 |
|
|
Python: Old-style to New-style Class Conversion Causes Error
I use the copy module later, in code I removed to reduce the amount you have to read.
EDIT: Nevermind. Either I misunderstood what I read or I didn't remember it correctly. I now know that __init__ isn't depreciated in 3+ Code:
class grid(list):
def __init__(self, size):
self.size = size;
for i in range(size):
temp = list();
for j in range(size):
temp.append(False);
self.append(temp);
|
| Dec12-12, 07:10 AM | #5 |
|
|
This example might help you http://stackoverflow.com/questions/2...rom-str-or-int
|
| New Reply |
| Thread Tools | |
Similar Threads for: Python: Old-style to New-style Class Conversion Causes Error
|
||||
| Thread | Forum | Replies | ||
| Python Vector Class no attribute error | Engineering, Comp Sci, & Technology Homework | 1 | ||
| Life style | Social Sciences | 5 | ||
| Scientific style | Academic Guidance | 4 | ||