Removing __slots__
~~~~~~~~~~~~~~~~~~~
To do this nicely requires the renaming of __dict__ to, say,
__attribs__ , ( since dict is unnecessarily unspecific, this seem like a
small improvement in itself. ) then using the setting of __attribs__ to
a non-mutable type (ie tuple of attribute names) to indicate the
behaviour of __slots__, rendering it unnecessary, this I think is a good
simplification. 6 2178
The point is to combine __dict__ and __slots__ into a new __attribs__,
the distinction being the type of __attribs__. If you don't specify
__attribs__ in the class you get the default __dict__ behavior, if you
do, and use a tuple, then you get the __slots__ behavior, and you can
easily tell which by checking the type, you could also iterate over the
attributes without caring which it was.
simon place <si*********@whsmithnet.co.uk> writes: The point is to combine __dict__ and __slots__ into a new __attribs__, the distinction being the type of __attribs__. If you don't specify __attribs__ in the class you get the default __dict__ behavior, if you do, and use a tuple, then you get the __slots__ behavior, and you can easily tell which by checking the type, you could also iterate over the attributes without caring which it was.
So how is this different from the current situation? If you don't
specify __slots__, you get the default __dict__ behaviour, if you do,
and use a tuple, you get the __slots__ behaviour, and you can easily
tell which by checking the type.
You also have the case of both __slots__ and __dict__ being in a type,
and this is a useful case also.
Regards,
Martin
i can't think of a point for __slots__ except to save the overhead of a
dict, this is why you DON'T HAVE a __dict__ when __slots__ is defined.
__slots__ should generally be used to improve the performance/footprint
of small/transient classes, ( it also prevents new instance variables
but this appears to be more of a side effect.)
The point of the combining is to simplify, you know, based on the idea
that keeping the language simply ( and logical ) aids comprehension.
simon place <si*********@whsmithnet.co.uk> writes: i can't think of a point for __slots__ except to save the overhead of a dict, this is why you DON'T HAVE a __dict__ when __slots__ is defined.
No. Some classes have slots for efficiency, and their subclasses have
dictionaries for generality.
Likewise, some classes have slots to save the dictionary for most
instances, but some instances may need additional attributes, in which
case Python creates the dictionary on-the-fly.
The point of the combining is to simplify, you know, based on the idea that keeping the language simply ( and logical ) aids comprehension.
I know.
Regards,
Martin
> No. Some classes have slots for efficiency, and their subclasses have dictionaries for generality.
Likewise, some classes have slots to save the dictionary for most instances, but some instances may need additional attributes, in which case Python creates the dictionary on-the-fly.
I know subclasses can add a __dict__, but i really thought a class with
__slots__ could not have a __dict__, doesn't the script below show this
behavior?
PythonWin 2.3b2 (#43, Jun 29 2003, 16:43:04) [MSC v.1200 32 bit (Intel)]
on win32.
Portions Copyright 1994-2001 Mark Hammond (mh******@skippinet.com.au) -
see 'Help/About PythonWin' for further copyright information. class A(object):
.... __slots__=['a']
.... b=A() b
<__main__.A object at 0x00EFABF0> b.__dict__
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'A' object has no attribute '__dict__' b.a
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: a b.a=1 b.a
1 b.b=1
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'A' object has no attribute 'b'
"simon place" <si*********@whsmithnet.co.uk> schrieb im Newsbeitrag
news:3f**********@news1.vip.uk.com... No. Some classes have slots for efficiency, and their subclasses have dictionaries for generality.
Likewise, some classes have slots to save the dictionary for most instances, but some instances may need additional attributes, in which case Python creates the dictionary on-the-fly.
I know subclasses can add a __dict__, but i really thought a class with __slots__ could not have a __dict__, doesn't the script below show this behavior?
<snip>
Consider this:
C:\Dokumente und Einstellungen\Administrator>python
Python 2.3a2 (#39, Feb 19 2003, 17:58:58) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information. class blah(object):
.... __slots__ = ['a','__dict__']
.... a = blah() a.__dict__
{} a.b = 5
which of course is kina wierd....
Ciao Ulrich This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: anabell |
last post by:
I have a code like this:
sqlString = 'INSERT INTO ' + self.TableName + ' VALUES (' + self.TableFields + ')'
self.cursor.execute(sqlString, self.__dict__)
This works correctly. However, I'm...
|
by: flori |
last post by:
i try to greate somthing like this
class ca(object): __slots__ = ("a",)
class cb(ca): __slots__ = ("a","b")
class cc(ca): __slots__ = ("a","c")
class cd(cb,cc): __slots__ = ("a","b","c","d")
...
|
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...
|
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...
|
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...
|
by: Ewald R. de Wit |
last post by:
I'm running into a something unexpected for a new-style class
that has both a class attribute and __slots__ defined. If the
name of the class attribute also exists in __slots__, Python
throws an...
|
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):
|
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...
|
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...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |