I find the following obscure (to me at least) use of type() useful exactly for this "bag of attributes" use case:
>>> employee = type("Employee", (object,), {})
>>> employee.name = "John Doe"
>>> employee.position = "Python programmer"
>>> employee.name, employee.position, employee
('John Doe', 'Python programmer', <class '__main__.Employee'>)
>>> details = dict(name="John Doe", position="Python programmer")
>>> employee = type("Employee", (object,), details)
>>> employee.name, employee.position, employee
('John Doe', 'Python programmer', <class '__main__.Employee'>)
regards,
Albert-Jan
----- Original Message -----
> From: Peter Otten <__pet...@web.de>
> To: pytho...@python.org
> Cc:
> Sent: Sunday, August 3, 2014 11:37 AM
> Subject: Re: Correct type for a simple "bag of attributes" namespace object
>
> Albert-Jan Roskam wrote:
>
>> I find the following obscure (to me at least) use of type() useful exactly
>> for this "bag of attributes" use case:
>>>>> employee = type("Employee", (object,), {})
>>>>> employee.name = "John Doe"
>>>>> employee.position = "Python programmer"
>>>>> employee.name, employee.position, employee
>> ('John Doe', 'Python programmer', <class
> '__main__.Employee'>)
>
> Are you sure you know what you are doing? The above is equivalent to
>
>>>> class employee:
> ... name = "John Doe"
> ... position = "Python programmer"
> ...
>>>> employee.name, employee.position, employee
> ('John Doe', 'Python programmer', <class
> '__main__.employee'>)
>>>> type(employee)
> <class 'type'>
>
> Basically you are using classes as instances. While there is no fundamental
> difference between classes and instances in Python you'll surprise readers
> of your code and waste some space:
Yes, I know that it is equivalent, but I have always found it kind of ugly to use class() just to bundle a number of items. Like you are 'announcing OOP' (not sure how to put this into words), and then it's merely a simple bundle. Or maybe it's just me being silly, because it's even in the Python tutorial: https://docs.python.org/2/tutorial/classes.html#odds-and-ends
>>>> import sys
>>>> sys.getsizeof(employee)
> 976
>>>> class Employee: pass
> ...
>>>> employee = Employee()
>>>> employee.name = "John Doe"
>>>> employee.position = "Python programmer"
>>>> sys.getsizeof(employee)
> 64
Wow, I was not aware of that at all. So they are not equivalent after all.
----- Original Message -----
> From: Albert-Jan Roskam <fo...@yahoo.com.dmarc.invalid>
> To: Terry Reedy <tjr...@udel.edu>; "pytho...@python.org" <pytho...@python.org>
> Cc:
> Sent: Sunday, August 3, 2014 11:17 AM
> Subject: Re: Correct type for a simple "bag of attributes" namespace object
<snip>
>>>>> Right. The 'types' module provides a SimpleNamespace
> class
>> for the
>>>>> common "bag of attributes" use case::
>>>>>
>>>>> >>> import types
>>>>> >>> foo = types.SimpleNamespace()
>>>>> >>> foo.x = 3
>>>>> >>> foo
>>>>> namespace(x=3)
>>>>
>>>> This is too much for children (& beginners).
>>>>
>>>> But perhaps what I should be asking for is for a new built-in that
> does
>> what types.SimpleNamespace() does, so that without any import you can
> write,
>> say,
>>>>
>>>> foo = namespace(a=1, b=2)
>>>> # or
>>>> bar = namespace()
>>>> bar.a = 1
>
> I find the following obscure (to me at least) use of type() useful exactly for
> this "bag of attributes" use case:
>>>> employee = type("Employee", (object,), {})
>>>> employee.name = "John Doe"
>>>> employee.position = "Python programmer"
>>>> employee.name, employee.position, employee
> ('John Doe', 'Python programmer', <class
> '__main__.Employee'>)
>
>>>> details = dict(name="John Doe", position="Python
> programmer")
>>>> employee = type("Employee", (object,), details)
>>>> employee.name, employee.position, employee
> ('John Doe', 'Python programmer', <class
> '__main__.Employee'>)
PS to my previous mail: class() can (should?) be used here to do the exact same thing but it feels a little like "Getting your car [OOP] just because you need an ashtray [bundled items]". :-)