473,507 Members | 11,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

__slots__ and copy again: why does it work?

I remember from painful experience that copy.copy() won't really copy
__slots__ members. But I have trouble explaning why the following code
works:

--- START---
#!/usr/bin/env python

import copy
class Foo (object):
__slots__ = 'i'

def __init__ (self):
self.i = 10
class Bar (Foo):
__slots__ = 'j'

def __init__ (self):
self.j = 20
f1 = Foo()
f2 = copy.copy(f1)

print f2.i # why does it work?
b1 = Bar()
b2 = copy.copy(b1)

print b2.j # why does it work?
print b2.i # doesn't work, as expected

--- END---
Any insight is welcome!

Dec 26 '05 #1
5 1439
I should've mentioned this was tested on Python 2.4.2.
fortepianissimo wrote:
I remember from painful experience that copy.copy() won't really copy
__slots__ members. But I have trouble explaning why the following code
works:

--- START---
#!/usr/bin/env python

import copy
class Foo (object):
__slots__ = 'i'

def __init__ (self):
self.i = 10
class Bar (Foo):
__slots__ = 'j'

def __init__ (self):
self.j = 20
f1 = Foo()
f2 = copy.copy(f1)

print f2.i # why does it work?
b1 = Bar()
b2 = copy.copy(b1)

print b2.j # why does it work?
print b2.i # doesn't work, as expected

--- END---
Any insight is welcome!


Dec 26 '05 #2
More weird observations: the following code does not work until you
change the name of the member 'longer' to a one-char name, for example,
'j':
--- START ---
#!/usr/bin/env python

import copy
class Foo (object):
__slots__ = 'i'
class Bar (Foo):
__slots__ = 'longer'
#__slots__ = 'j'
b1 = Bar()
b1.longer = 22
#b1.j = 22

b2 = copy.copy(b1)
# doesn't work in Python 2.4.2
# BUT if 'longer' is changed to 'j' in the entire file, then it works!
print b2.longer
#print b2.j

--- END ---
I've tried different names and concluded that as long as I used one
character the code works. Anything longer than one character bombs.

Why?

Dec 27 '05 #3
Mystery solved - when there's only one slot I should've used __slots__
= ('i', ). Duh!

So in short, __slots__ and copy.copy() work fine in Python 2.4.2.

Dec 27 '05 #4
26 Dec 2005 20:33:35 -0800, fortepianissimo <fo*************@gmail.com>:
Mystery solved - when there's only one slot I should've used __slots__
= ('i', ). Duh!

So in short, __slots__ and copy.copy() work fine in Python 2.4.2.

Hard works, but very useful :)
--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit
Dec 27 '05 #5
To be complete, the first code snippet, when modified as follows, works
fine in Python 2.4.2:

--- START ---
#!/usr/bin/env python

import copy

class Foo (object):
__slots__ = ('i', )

def __init__ (self):
self.i = 10

class Bar (Foo):
__slots__ = ('j', )

def __init__ (self):
super(Bar, self).__init__()
self.j = 20

f1 = Foo()
f2 = copy.copy(f1)

print f2.i # works

b1 = Bar()
b2 = copy.copy(b1)

print b2.j # works
print b2.i # works
--- END ---

The last line didn't work last time because b2.i was never initialized.

Dec 27 '05 #6

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

Similar topics

5
2912
by: Jean Brouwers | last post by:
Classes using __slots__ seem to be quite a bit smaller and faster to instantiate than regular Python classes using __dict__. Below are the results for the __slots__ and __dict__ version of a...
3
1629
by: Nick Jacobson | last post by:
The __slots__ attribute of new-style classes can reduce memory usage when there are millions of instantiations of a class. So would a __slots__ attribute for functions/methods have a similar...
7
1651
by: Porky Pig Jr | last post by:
Hello, I"m still learning Python, but going through the Ch 5 OOP of Nutshell book. There is discussion on __slots__, and my understanding from reading this section is that if I have a class...
4
1486
by: dan.j.weber | last post by:
I see some programs declaring the names of class variables in "__slots__". I've looked this up and the docs say something about old and new style classes, whatever that means. Can someone give me a...
3
1560
by: Schüle Daniel | last post by:
Hello, consider this code >>> class A(object): .... def __init__(self): .... self.a = 1 .... self.b = 2 .... >>> class B(A):
15
531
by: David Isaac | last post by:
1. "Without a __dict__ variable, instances cannot be assigned new variables not listed in the __slots__ definition." So this seemed an interesting restriction to impose in some instances, but...
1
2693
by: pascal.parent | last post by:
Hi, I try to define a (new-style) class who: - have a __slots__ defined to be strict attributes, - return None if the attribute is 'ok' but not set, or raise a 'normal' error if the attribute...
3
1609
by: John Machin | last post by:
I have stumbled across some class definitions which include all/most method names in a __slots__ "declaration". A cut-down and disguised example appears at the end of this posting. Never mind...
27
1697
by: Licheng Fang | last post by:
Python is supposed to be readable, but after programming in Python for a while I find my Python programs can be more obfuscated than their C/C ++ counterparts sometimes. Part of the reason is that...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.