On Thu, Jan 18, 2018 at 05:14:43PM +, Albert-Jan Roskam wrote:
> Is a metaclass the best/preferred/only way of doing this? Or is a
> class decorator an alternative route?
I haven't thought deeply about this, but I suspect a class decorator
should do the job too.
The general advice is to us
On Thu, Jan 18, 2018 at 05:31:24PM +, Albert-Jan Roskam wrote:
> > Don't make the mistake of doing this:
> >
> > from collections import namedtuple
> > a = namedtuple('Bag', 'yes no dunno')(yes=1, no=0, dunno=42)
> > b = namedtuple('Bag', 'yes no dunno')(yes='okay', no='no way', dunno='not a
On Jan 10, 2018 19:32, Peter Otten <__pete...@web.de> wrote:
>
> Albert-Jan Roskam wrote:
>
> > Why does following the line (in #3)
>
> > # 3-
> > class Meta(type):
> > def __new__(cls, name, bases, attrs):
> > for attr, obj in attrs.item
On Jan 10, 2018 18:57, Steven D'Aprano wrote:
>
> On Wed, Jan 10, 2018 at 04:08:04PM +, Albert-Jan Roskam wrote:
>
> > In another thread on this list I was reminded of
> > types.SimpleNamespace. This is nice, but I wanted to create a bag
> > class with constants that are read-only.
>
> If you
Steven D'Aprano wrote:
> On Wed, Jan 10, 2018 at 07:29:58PM +0100, Peter Otten wrote:
>
> [...]
>> elif not isinstance(obj, property):
>> attrs[attr] = property(lambda self, obj=obj: obj)
>
>> PS: If you don't remember why the obj=obj is necessary:
>> Python uses late
On Wed, Jan 10, 2018 at 07:29:58PM +0100, Peter Otten wrote:
[...]
> elif not isinstance(obj, property):
> attrs[attr] = property(lambda self, obj=obj: obj)
> PS: If you don't remember why the obj=obj is necessary:
> Python uses late binding; without that trick all lam
Albert-Jan Roskam wrote:
> Why does following the line (in #3)
> # 3-
> class Meta(type):
> def __new__(cls, name, bases, attrs):
> for attr, obj in attrs.items():
> if attr.startswith('_'):
> continue
>
On Wed, Jan 10, 2018 at 10:08 AM, Albert-Jan Roskam
wrote:
> Hi,
>
>
> In another thread on this list I was reminded of types.SimpleNamespace. This
> is nice, but I wanted to create a bag class with constants that are
> read-only. My main question is about example #3 below (example #2 just
> il
On Wed, Jan 10, 2018 at 04:08:04PM +, Albert-Jan Roskam wrote:
> In another thread on this list I was reminded of
> types.SimpleNamespace. This is nice, but I wanted to create a bag
> class with constants that are read-only.
If you expect to specify the names of the constants ahead of time,
anil maran wrote:
> hi pygurus
> can you please tell me why we need metaclasses and how to use them
Hmm...metaclasses are an advanced topic, first exposure to them usually
causes one's brain to explode. Fortunately the condition is only
temporary :-)
Basically a metaclass is the type of a class
10 matches
Mail list logo