Hi:
I am puzzled about the following piece of code which attempts to create
a class that can be used as record or struct with a limited set of
allowed attributes that can be set into an instance of the class.
class RecordClass(object):
__slots__ = ["foo"]
def __init__(self, args):
print "Initial slots = ", RecordClass.__slots__
RecordClass.__slots__ = list(args)
print "Final slots = ", RecordClass.__slots__
pass
def new_record(slotlist):
return RecordClass(slotlist)
if __name__ == "__main__":
record1 = new_record(["age", "name", "job"])
record1.age = 27
record1.name = 'Fred'
record1.job = 'Plumber'
record1.salary = 50000
When executed I get:
Initial slots = ['foo']
Final slots = ['age', 'name', 'job']
Traceback (most recent call last):
File "D:\ProgrammingProjects\JustForTesting\recordclass es.py", line
37, in ?
record1.age = 27
AttributeError: 'RecordClass' object has no attribute 'age'
I don't understand why I cannot set an attribute 'age' into record1.
Thanks,
Don. 2 1201
On Sun, 26 Feb 2006 17:01:45 -0500, Don Taylor wrote: Hi:
I am puzzled about the following piece of code which attempts to create a class that can be used as record or struct with a limited set of allowed attributes that can be set into an instance of the class.
class RecordClass(object): __slots__ = ["foo"]
You define a RecordClass with a single slot, "foo".
[snip]
I don't understand why I cannot set an attribute 'age' into record1.
Because you defined the class with only a slot for "foo", not "age".
Re-defining the __slots__ attribute at runtime does not add or subtract
slots from the class. It just breaks your class.
If you want to add and delete arbitrary attributes at runtime, don't use
slots.
--
Steven.
Steven is right, however, there is a way:
def new_record(slotlist):
class R(object):
__slots__ = slotlist
return R()
record1 = new_record(["age", "name", "job"])
record1.age = 27
record1.name = 'Fred'
record1.job = 'Plumber'
record1.salary = 50000 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: 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...
|
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: 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: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |