Re: Using object as a class

2018-03-26 Thread Steven D'Aprano
On Mon, 26 Mar 2018 07:31:06 -0500, D'Arcy Cain wrote:

> Is this behaviour (object not quite like a class) documented anywhere?

It's exactly like a class. It's an immutable class. You are making 
assumptions about what classes must be able to do.


> Does anyone know the rationale for this if any?

object is intended as the base class for all others. As such, it is 
absolutely dead simple, with no state and very few methods other than 
those required by everything. The usual way we use object instances is as 
identity-objects: objects with (almost) no behaviour other than their 
identity:

SENTINEL = object()
# later
if obj is SENTINEL: ...

A bit like None.

The usual way to get what you want is a simple subclass of object, but 
even more convenient:

py> from types import SimpleNamespace
py> x = SimpleNamespace(x=1, y=2, z=3)
py> x
namespace(x=1, y=2, z=3)




-- 
Steve

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Using object as a class

2018-03-26 Thread Paul Moore
In fact, object acts just like a user-defined class, with __slots__
set to empty:

>>> class MyObj(object):
... __slots__ = ()
...
>>> o = MyObj()
>>> o.x = 3
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'MyObj' object has no attribute 'x'

See https://docs.python.org/3.6/reference/datamodel.html#slots

IIRC, most built in objects (of which object is one) behave as if they
have __slots__ set (they don't actually, because they are written in
C, but the effect is the same).

Paul

On 26 March 2018 at 13:31, D'Arcy Cain  wrote:
> It's called a super class but it doesn't quite work like a normal class.
>
 OBJ = object()
 OBJ.x = 3
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: 'object' object has no attribute 'x'
>
> I can fix this by creating a NULL class.
>
 class NullObject(object): pass
> ...
 OBJ = NullObject()
 OBJ.x = 3
 OBJ.x
> 3

>
> Is this behaviour (object not quite like a class) documented anywhere?
> Does anyone know the rationale for this if any?
>
> In case anyone wants to know why I am doing this, sometimes I simply
> want an object to hold values that I can pass around.  I don't need
> methods and I don't always know what variables I am going to need.
>
> And yes, I know that dict is the usual way to do this.
>
> --
> D'Arcy J.M. Cain
> Vybe Networks Inc.
> http://www.VybeNetworks.com/
> IM:da...@vex.net VoIP: sip:da...@vybenetworks.com
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Using object as a class

2018-03-26 Thread D'Arcy Cain
It's called a super class but it doesn't quite work like a normal class.

>>> OBJ = object()
>>> OBJ.x = 3
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'object' object has no attribute 'x'

I can fix this by creating a NULL class.

>>> class NullObject(object): pass
...
>>> OBJ = NullObject()
>>> OBJ.x = 3
>>> OBJ.x
3
>>>

Is this behaviour (object not quite like a class) documented anywhere?
Does anyone know the rationale for this if any?

In case anyone wants to know why I am doing this, sometimes I simply
want an object to hold values that I can pass around.  I don't need
methods and I don't always know what variables I am going to need.

And yes, I know that dict is the usual way to do this.

-- 
D'Arcy J.M. Cain
Vybe Networks Inc.
http://www.VybeNetworks.com/
IM:da...@vex.net VoIP: sip:da...@vybenetworks.com
-- 
https://mail.python.org/mailman/listinfo/python-list