Re: Is it possible to inheret a metaclass.

2020-04-11 Thread Peter Otten
Antoon Pardon wrote: > > > Op 9/04/20 om 18:37 schreef Peter Otten: >> Antoon Pardon wrote: >> >>> I am experimenting with subclasses that all need the same metaclass as >>> the base class. Is there a way to make the metaclass be inherited, so >>&

Re: Is it possible to inheret a metaclass.

2020-04-11 Thread Pieter van Oostrum
Pieter van Oostrum writes: > Your Pardon is not a class, it is a function. Class A is created by > type(cls, *args), so 'type' is the metaclass of A, and therefore also of > B. > Creation of B does not call Pardon. With a class it *does* work: In [74]: class Pardon(t

Re: Is it possible to inheret a metaclass.

2020-04-10 Thread Greg Ewing via Python-list
On 11/04/20 12:19 am, Pieter van Oostrum wrote: Your Pardon is not a class, it is a function. To elaborate on that a bit, the way inheritance of metaclasses works is that when you define a class, if you don't explicity specify a metaclass, it uses the class of the base class as the meta

Re: Is it possible to inheret a metaclass.

2020-04-10 Thread Pieter van Oostrum
Antoon Pardon writes: > Op 9/04/20 om 18:37 schreef Peter Otten: >> Antoon Pardon wrote: >> >>> I am experimenting with subclasses that all need the same metaclass as the >>> base class. Is there a way to make the metaclass be inherited, so that you >&g

Re: Is it possible to inheret a metaclass.

2020-04-10 Thread Antoon Pardon
Op 9/04/20 om 18:37 schreef Peter Otten: Antoon Pardon wrote: I am experimenting with subclasses that all need the same metaclass as the base class. Is there a way to make the metaclass be inherited, so that you don't have to repeat the "metaclass = MetaClass" with every subc

Re: Is it possible to inheret a metaclass.

2020-04-09 Thread Peter Otten
Antoon Pardon wrote: > I am experimenting with subclasses that all need the same metaclass as the > base class. Is there a way to make the metaclass be inherited, so that you > don't have to repeat the "metaclass = MetaClass" with every subclass. ? This is not only possi

Is it possible to inheret a metaclass.

2020-04-09 Thread Antoon Pardon
I am experimenting with subclasses that all need the same metaclass as the base class. Is there a way to make the metaclass be inherited, so that you don't have to repeat the "metaclass = MetaClass" with every subclass. -- Antoon Pardon. -- https://mail.python.org/mailman/listinfo/python-list

Re: Better use a class decorator or a metaclass?(was: super not behaving as I expected)

2020-04-04 Thread Souvik Dutta
> slots = C.__slots__ > > except AttributeError: > > assert C is object > > else: > > all_slots.update(slots) > > return all_slots > > > > I have been thinking about

Better use a class decorator or a metaclass?(was: super not behaving as I expected)

2020-04-04 Thread Antoon Pardon
try: > slots = C.__slots__ > except AttributeError: > assert C is object > else: > all_slots.update(slots) > return all_slots > I have been thinking about this. AFAIU the slots are static data. So

Re: Is it dangeous when using custom metaclass?

2018-10-17 Thread dieter
jf...@ms4.hinet.net writes: > ... > Hard to find the document of type.__init__. I can only guess it does nothing, > at least no thing serious, to avoid trouble the metaclass's __init__ may > cause in a class hierarchy:-) You always have the possibility to look at the source. All classes have co

Re: Is it dangeous when using custom metaclass?

2018-10-17 Thread jfong
dieter at 2018/10/17 UTC+8 PM 1:15:01 wrote: > jf...@ms4.hinet.net writes: > > Gregory Ewing at 2018/10/16 UTC+8 PM 2:01:01 wrote > >> jf...@ms4.hinet.net wrote: > >> > class Structure(metaclass=StructureMeta): ... > >> > > >> > class PolyHea

Re: Is it dangeous when using custom metaclass?

2018-10-16 Thread dieter
jf...@ms4.hinet.net writes: > Gregory Ewing at 2018/10/16 UTC+8 PM 2:01:01 wrote >> jf...@ms4.hinet.net wrote: >> > class Structure(metaclass=StructureMeta): ... >> > >> > class PolyHeader(Structure): ... >> > >> > As my understanding

Re: Is it dangeous when using custom metaclass?

2018-10-16 Thread jfong
Gregory Ewing at 2018/10/16 UTC+8 PM 2:01:01 wrote > jf...@ms4.hinet.net wrote: > > class Structure(metaclass=StructureMeta): ... > > > > class PolyHeader(Structure): ... > > > > As my understanding, the metaclass's __init__ was called when a class was >

Re: Is it dangeous when using custom metaclass?

2018-10-15 Thread Gregory Ewing
jf...@ms4.hinet.net wrote: class Structure(metaclass=StructureMeta): ... class PolyHeader(Structure): ... As my understanding, the metaclass's __init__ was called when a class was created. In the above example, both the Structure and PolyHeader called it. My question is: because the PolyH

Is it dangeous when using custom metaclass?

2018-10-15 Thread jfong
class StructureMeta(type): def __init__(self, clsname, bases, clsdict): offset = 0 ... ... setattr(self, 'struct_size', offset) class Structure(metaclass=StructureMeta): ... ... class PolyHeader(Structure): ... ... As my understa

RE: Single DB connection during class's lifetime. Metaclass,singletonand __new__() examples and references.

2018-10-15 Thread Ryan Johnson
Thank you. That clears everything up. Sent from Mail for Windows 10 From: Cameron Simpson Sent: Saturday, October 13, 2018 6:06 PM To: Ryan Johnson Cc: python-list@python.org Subject: Re: Single DB connection during class's lifetime. Metaclass,singletonand __new__() examples and references

Re: Single DB connection during class's lifetime. Metaclass,singleton and __new__() examples and references.

2018-10-13 Thread Cameron Simpson
On 12Oct2018 13:28, Ryan Johnson wrote: Thanks for the clarification. If I am creating a class variable, are you suggesting I perform the “if it exists, great, otherwise make it” logic in the __init__ block or in the class definition block? Will that even run in a class definition? The clas

RE: Single DB connection during class's lifetime. Metaclass,singleton and __new__() examples and references.

2018-10-12 Thread Ryan Johnson
AM To: python-list@python.org Subject: Re: Single DB connection during class's lifetime. Metaclass,singleton and __new__() examples and references. On 12/10/2018 01:19, Ryan Johnson wrote: > I am working on using mysql.connector in a class and have found an example of > how to crea

Re: Single DB connection during class's lifetime. Metaclass, singleton and __new__() examples and references.

2018-10-12 Thread Thomas Jollans
On 12/10/2018 01:19, Ryan Johnson wrote: I am working on using mysql.connector in a class and have found an example of how to create a single connection that spans the lifetime of all instances of the class: https://softwareengineering.stackexchange.com/a/358061/317228 however, I do not under

Re: Single DB connection during class's lifetime. Metaclass, singleton and __new__() examples and references.

2018-10-12 Thread dieter
Ryan Johnson writes: > I am working on using mysql.connector in a class and have found an example of > how to create a single connection that spans the lifetime of all instances of > the class: > > https://softwareengineering.stackexchange.com/a/358061/317228 > > however, I do not understand a

Single DB connection during class's lifetime. Metaclass, singleton and __new__() examples and references.

2018-10-11 Thread Ryan Johnson
I am working on using mysql.connector in a class and have found an example of how to create a single connection that spans the lifetime of all instances of the class: https://softwareengineering.stackexchange.com/a/358061/317228 however, I do not understand a few things about the class, includ

Creation of a metaclass for dataclass.

2017-12-29 Thread Kirill Balunov
elds'] = tuple(clsdict['__annotations__'].items()) _cls = type.__new__(metacls, cls, bases, clsdict) return _cls And test class: class MyClass(metaclass=DataMeta): a: float barattr: int = 2 jik = 12 bzik: int =14 def foo(self, param):

Re: Metaclass conundrum - binding value from an outer scope

2017-04-22 Thread Peter Otten
Skip Montanaro wrote: >> Another round, this time with a metaclass. As you have found partial() >> does not work as a method because it's not a descriptor (i. e. no >> __get__() method). Instead you can try a closure: >> >> def make_method(a): >>

Re: Metaclass conundrum - binding value from an outer scope

2017-04-22 Thread Skip Montanaro
> Another round, this time with a metaclass. As you have found partial() does > not work as a method because it's not a descriptor (i. e. no __get__() > method). Instead you can try a closure: > > def make_method(a): > underlying = getattr(SomeOtherClass, a) > @

Re: Metaclass conundrum - binding value from an outer scope

2017-04-21 Thread Peter Otten
Skip Montanaro wrote: > On Thu, Apr 20, 2017 at 3:19 PM, Peter Otten <__pete...@web.de> wrote: > >> If being helpful really is the only purpose of the metaclass you can >> implement a SomeClass.__dir__() method instead: >> >> def __dir__(self): &g

Re: Metaclass conundrum - binding value from an outer scope

2017-04-21 Thread Skip Montanaro
2017-04-20 15:55 GMT-05:00 Lele Gaifax : > Does > > underlying = getattr(SomeOtherClass, a) > def _meth(self, *args, _underlying=underlying): > return _underlying(self._instance, *args) > > help? > Hi, Lele. Long time no chat... I thought of that, but with _underlying declared af

Re: Metaclass conundrum - binding value from an outer scope

2017-04-21 Thread Skip Montanaro
On Thu, Apr 20, 2017 at 3:19 PM, Peter Otten <__pete...@web.de> wrote: > If being helpful really is the only purpose of the metaclass you can > implement a SomeClass.__dir__() method instead: > > def __dir__(self): > names = dir(self._instance) > #

Re: Metaclass conundrum - binding value from an outer scope

2017-04-20 Thread Lele Gaifax
Skip Montanaro writes: > underlying = getattr(SomeOtherClass, a) > def _meth(self, *args): > return underlying(self._instance, *args) Does underlying = getattr(SomeOtherClass, a) def _meth(self, *args, _underlying=underlying): return _unde

Re: Metaclass conundrum - binding value from an outer scope

2017-04-20 Thread Peter Otten
key): > return getattr(self._instance, key) > > ... and so on ... > > If someone tries help(SomeClass) or dir(SomeClass) today, none of the > attributes or docstrings defined in SomeOtherClass are shown. If being helpful really is the only purpose of the metaclass you

Metaclass conundrum - binding value from an outer scope

2017-04-20 Thread Skip Montanaro
(SomeClass) or dir(SomeClass) today, none of the attributes or docstrings defined in SomeOtherClass are shown. It was almost a straightforward exercise to write a metaclass which defines methods on SomeClass which delegate to the underlying method on SomeOtherClass. My problem is evaluating the return

Re: Substitute a mock object for the metaclass of a class

2017-03-13 Thread Matt Wheeler
A small correction... On Mon, 13 Mar 2017 at 22:36 Matt Wheeler wrote: > ``` > from unittest.mock import patch > > import lorem > > > @patch('lorem.type') > def test_things(mocktype): > lorem.quux(metameta.Foo()) > > lorem.return_value.assert_called_with() > this line should of course re

Re: Substitute a mock object for the metaclass of a class

2017-03-13 Thread Matt Wheeler
On Mon, 13 Mar 2017 at 00:52 Ben Finney wrote: > How can I override the metaclass of a Python class, with a > `unittest.mock.MagicMock` instance instead? > At first I misunderstood what you were looking for, and was about to reply to the effect of "you're too late, the me

Substitute a mock object for the metaclass of a class

2017-03-12 Thread Ben Finney
How can I override the metaclass of a Python class, with a `unittest.mock.MagicMock` instance instead? I have a function whose job involves working with the metaclass of an argument:: # lorem.py class Foo(object): pass def quux(existing_class): … metaclass

Re: Metaclass to rewrite class __doc__?

2017-02-23 Thread Peter Otten
Skip Montanaro wrote: > I stumbled upon this use of a metaclass to modify function docstrings: > > http://www.jesshamrick.com/2013/04/17/rewriting-python-docstrings-with-a-metaclass/ > > In certain circumstances (mostly when extending some Pybind11 wrappers), > it might be nic

Metaclass to rewrite class __doc__?

2017-02-23 Thread Skip Montanaro
I stumbled upon this use of a metaclass to modify function docstrings: http://www.jesshamrick.com/2013/04/17/rewriting-python-docstrings-with-a-metaclass/ In certain circumstances (mostly when extending some Pybind11 wrappers), it might be nice to extend/modify a class's docstring in a si

metaclass

2016-02-04 Thread ast
Hi I am looking the relationship between some classes from the enum module from enum import EnumMeta, Enum class Color(Enum): pass type(EnumMeta) EnumMeta.__bases__ (,) so EnumMeta is a metaclass, it is an instance of type and inherit from type too. type(Enum

Re: Arguments for "type metaclass" __init__ method

2015-06-22 Thread Chris Angelico
dict) as args. > > > But in case of creating class via class definition("type" act as default > metaclass), the type is called by following syntax, > > type(cls, classname, bases, attrbs) > > My doubt is how it possible to pass "cls" as argument(as class is n

Arguments for "type metaclass" __init__ method

2015-06-22 Thread Kaviraj Kanagaraj
Hi All, I was reading about meta programming in "Pro Django" book. I came across the "type" class which will be acting as default meta class for all other class. Also "type" metaclass is where actuall class object is created To dynamically create a class: Dyn

Re: Metaclass/abc hackery

2013-10-11 Thread Marco Buttu
ing like that's available... Not likely something that would be used very often, but would likely sometimes be useful. Thanks, I am not sure about your question, but I try to explain a bit some possibilities. If you define a __setattr__ method in the metaclass, then you can intercept the a

Metaclass/abc hackery

2013-10-11 Thread Demian Brecht
As with most I'm sure, short of using abc's, I've had very little exposure to metaclasses. So, when digging into abc implementation, I figured it would be a good idea to dig into metaclasses, their usage and actually try writing one. What I did may be contrived, but it was fun nonetheless and a go

Re: Overriding of the type.__call__() method in a metaclass

2013-10-06 Thread Marco Buttu
is correct. The "class FooMeta(type)..." statement is syntactic sugar for type.__call__(...). But your description is incorrect. This doesn't occur when you "call the metaclass type"... Oh damn! Your are right :) Thanks to you and Peter. Now (I hope...) it should be c

Re: Overriding of the type.__call__() method in a metaclass

2013-10-06 Thread Steven D'Aprano
On Sun, 06 Oct 2013 20:17:33 +0200, Marco Buttu wrote: > Hi all, I have a question about class creation and the __call__ method. > I have the following metaclass: > > >>> class FooMeta(type): > ... def __call__(metacls, name, bases, namespace): > ...prin

Re: Overriding of the type.__call__() method in a metaclass

2013-10-06 Thread Peter Otten
Marco Buttu wrote: > Hi all, I have a question about class creation and the __call__ method. > I have the following metaclass: > > >>> class FooMeta(type): > ... def __call__(metacls, name, bases, namespace): > ... print("FooMeta.__call__()") &

Overriding of the type.__call__() method in a metaclass

2013-10-06 Thread Marco Buttu
Hi all, I have a question about class creation and the __call__ method. I have the following metaclass: >>> class FooMeta(type): ... def __call__(metacls, name, bases, namespace): ... print("FooMeta.__call__()") From what I undestood, at the end of the clas

Re: Is a Metaclass the appropriate way to solve this problem?

2013-08-07 Thread Ian Kelly
all the attributes which are Database* descriptors)? > I know that I could eschew metaclasses altogether and use a common > super-class, but this doesn't feel like the right situation for inheritance > to me. Is a metaclass going to be the cleanest and easiest-to-understand way > to solve

Is a Metaclass the appropriate way to solve this problem?

2013-08-07 Thread Matthew Lefavor
All: Like most people, I find the whole metaclass topic pretty obscure, and I have avoided trying to use one for a while. I am also aware of Tim Peter's famous advice that if you have to ask whether you need a metaclass, then you almost certainly don't. But in this case I know I am

Re: How to define metaclass for a class that extends from sqlalchemy declarative base ?

2013-07-02 Thread Peter Otten
= name > self.description = description > self.is_active = is_active > > def __repr__(self): > return "<(%d, '%s', '%s', %r)>" % (self.id, self.name, > self.description, self.isactive) > > And

How to define metaclass for a class that extends from sqlalchemy declarative base ?

2013-07-02 Thread Ven
return "<(%d, '%s', '%s', %r)>" % (self.id, self.name, self.description, self.isactive) And the error I am getting is this: TypeError: Error when calling the metaclass bases metaclass conflict: the metaclass of a derived class must be a (no

Re: Emulating C++ namespaces with ChainMap and metaclass trickery

2012-10-03 Thread Mark Adam
me in separate files. > > Using Python 3.3's ChainMap type, and some metaclass trickery, I abuse > the class keyword to (almost) emulate C++ namespaces: > Very interesting. I like the idea of continuing the namespace meme. My idea of using the builtins (in the prior list thread of &qu

Emulating C++ namespaces with ChainMap and metaclass trickery

2012-10-03 Thread Steven D'Aprano
C++ namespaces are useful for encapsulating related objects within a single file, subdividing the global namespace without using classes. Python has modules, but they come in separate files. Using Python 3.3's ChainMap type, and some metaclass trickery, I abuse the class keyword to (a

Re: metaclass question

2012-09-24 Thread Ian Kelly
On Mon, Sep 24, 2012 at 11:43 AM, Chris Withers wrote: > Hi All, > > Is there a metaclass-y way I could cause the following: > > class TheParser(Parser): > def handle_ARecord(self): > pass > def handle_ARecord(self): > pass > > ...to rais

metaclass question

2012-09-24 Thread Chris Withers
Hi All, Is there a metaclass-y way I could cause the following: class TheParser(Parser): def handle_ARecord(self): pass def handle_ARecord(self): pass ...to raise an exception as a result of the 'handle_ARecord' name being reused? cheers, Chris --

Re: Style question: metaclass self vs cls?

2012-07-17 Thread Steven D'Aprano
On Tue, 17 Jul 2012 05:23:22 -0700, Michele Simionato wrote: > The standard is to use `cls`. In the __new__ method you can use `mcl` or > `meta`. Thanks to everyone who answered. I think I will stick with "meta" and "cls". -- Steven -- http://mail.python.org/mailman/listinfo/python-list

Re: Style question: metaclass self vs cls?

2012-07-17 Thread Ian Kelly
On Tue, Jul 17, 2012 at 12:10 AM, alex23 wrote: > On Jul 17, 1:29 am, Steven D'Aprano +comp.lang.pyt...@pearwood.info> wrote: >> Here's a style question for you: in a metaclass, what should I call the >> instance parameter of methods, "cls" or "

Re: Style question: metaclass self vs cls?

2012-07-17 Thread Ian Kelly
On Tue, Jul 17, 2012 at 6:23 AM, Michele Simionato wrote: > The standard is to use `cls`. In the __new__ method you can use `mcl` or > `meta`. I've also seen `mcs` a fair amount. -- http://mail.python.org/mailman/listinfo/python-list

Re: Style question: metaclass self vs cls?

2012-07-17 Thread Michele Simionato
The standard is to use `cls`. In the __new__ method you can use `mcl` or `meta`. -- http://mail.python.org/mailman/listinfo/python-list

Re: Style question: metaclass self vs cls?

2012-07-16 Thread Terry Reedy
On 7/16/2012 11:29 AM, Steven D'Aprano wrote: Here's a style question for you: in a metaclass, what should I call the instance parameter of methods, "cls" or "self"? class ExampleMeta(type): def method(self, *args): ... I'm not quite sure if that feels

Style question: metaclass self vs cls?

2012-07-16 Thread Steven D'Aprano
Here's a style question for you: in a metaclass, what should I call the instance parameter of methods, "cls" or "self"? class ExampleMeta(type): def method(self, *args): ... I'm not quite sure if that feels right. On the one hand, self is the ExampleMeta

Re: Metaclass of a metaclass

2012-06-05 Thread bruno.desthuilli...@gmail.com
On Jun 5, 10:48 am, Steven D'Aprano wrote: > Apparently it gives an error. Can anyone explain why this does not work? > > # Python 3.2 > > >>> class MyType(type):  # A metaclass... > > ...     def __repr__(self): > ...             s = super().__repr__() >

Re: Metaclass of a metaclass

2012-06-05 Thread Ian Kelly
On Tue, Jun 5, 2012 at 2:48 AM, Steven D'Aprano wrote: > I was playing around with metaclasses and I wondered what would happen if > the metaclass itself had a metaclass. Sort of a metametaclass. > > Apparently it gives an error. Can anyone explain why this does not work? In y

Re: Metaclass of a metaclass

2012-06-05 Thread Peter Otten
Steven D'Aprano wrote: > I was playing around with metaclasses and I wondered what would happen if > the metaclass itself had a metaclass. Sort of a metametaclass. > > Apparently it gives an error. Can anyone explain why this does not work? > > # Python 3.2 > &g

Metaclass of a metaclass

2012-06-05 Thread Steven D'Aprano
I was playing around with metaclasses and I wondered what would happen if the metaclass itself had a metaclass. Sort of a metametaclass. Apparently it gives an error. Can anyone explain why this does not work? # Python 3.2 >>> class MyType(type): # A metaclass... ... def __re

Re: pickling instances of metaclass generated classes

2012-01-03 Thread Ian Kelly
> Ok, > > After reading all posts (thanks a lot), I am considering to use the > following base metaclass for all metaclasses that must lead to > pickleable instances (not pickleable classes): > > > import sys > > class Meta(type): >def __new__(mcls, name, b

Re: pickling instances of metaclass generated classes

2012-01-03 Thread lars van gemerden
ickle cannot find the class definitions of the instances. I am trying > to add a line to the __new__ of the metaclass to add the new class > under the right name in the right module/place, so pickle can find > it. > > Is this the right approach? Can anyone explain to me where/how to

Re: pickling instances of metaclass generated classes

2012-01-01 Thread Peter Otten
lars van gemerden wrote: >> import pickle >> import sys >> >> class MetaClass(type): >> pass >> >> class M(object): >> def __init__(self, module): >> self.__module = module >> def __getattr__(self, name): >&

Re: pickling instances of metaclass generated classes

2011-12-30 Thread Ian Kelly
On Fri, Dec 30, 2011 at 9:51 AM, lars van gemerden wrote: > I still wonder whether it might be easier to add the class to the > namespace. Can anyone help me with that? from mypackage import mymodule setattr(mymodule, myclass.__name__, myclass) -- http://mail.python.org/mailman/listinfo/python-

Re: pickling instances of metaclass generated classes

2011-12-30 Thread lars van gemerden
. > > > > > Pickle cannot find the class definitions of the instances. I am trying > > > > to add a line to the __new__ of the metaclass to add the new class > > > > under the right name in the right module/place, so pickle can find > > > > it. &

Re: pickling instances of metaclass generated classes

2011-12-30 Thread lars van gemerden
ollowing: > > > > I am using metaclasses to make classes and these classes to make > > > instances. Now I want to use multiprocessing, which needs to pickle > > > these instances. > > > > Pickle cannot find the class definitions of the instances. I am trying >

Re: pickling instances of metaclass generated classes

2011-12-30 Thread Peter Otten
sses and these classes to make >> > instances. Now I want to use multiprocessing, which needs to pickle >> > these instances. >> >> > Pickle cannot find the class definitions of the instances. I am trying >> > to add a line to the __new__ of the metaclas

Re: pickling instances of metaclass generated classes

2011-12-30 Thread lars van gemerden
to use multiprocessing, which needs to pickle > > these instances. > > > Pickle cannot find the class definitions of the instances. I am trying > > to add a line to the __new__ of the metaclass to add the new class > > under the right name in the right module/place, so pi

Re: pickling instances of metaclass generated classes

2011-12-30 Thread lars van gemerden
to use multiprocessing, which needs to pickle > > these instances. > > > Pickle cannot find the class definitions of the instances. I am trying > > to add a line to the __new__ of the metaclass to add the new class > > under the right name in the right module/place, so pi

Re: pickling instances of metaclass generated classes

2011-12-29 Thread Ian Kelly
> Pickle cannot find the class definitions of the instances. I am trying > to add a line to the __new__ of the metaclass to add the new class > under the right name in the right module/place, so pickle can find > it. > > Is this the right approach? Can anyone explain to me where/how

Re: pickling instances of metaclass generated classes

2011-12-29 Thread Robert Kern
instances. I am trying to add a line to the __new__ of the metaclass to add the new class under the right name in the right module/place, so pickle can find it. Is this the right approach? Can anyone explain to me where/how to add these classes for pickle to find and maybe why? Can you post some

pickling instances of metaclass generated classes

2011-12-29 Thread lars van gemerden
__new__ of the metaclass to add the new class under the right name in the right module/place, so pickle can find it. Is this the right approach? Can anyone explain to me where/how to add these classes for pickle to find and maybe why? Thanks in advance, Lars -- http://mail.python.org/mailman/listinfo

Re: Question about metaclass

2011-11-01 Thread Makoto Kuwata
On Wed, Nov 2, 2011 at 1:40 PM, Ian Kelly wrote: > > If you want to customize the dict you need to do it in __new__, not > __init__.  By the time __init__ is called, the class has already been > created. > > class MetaClass(type): >    def __new__(mcs, name, bases, dict):

Re: Question about metaclass

2011-11-01 Thread Patrick Maupin
thod automatically, but I don't get result what I want. > (python 2.5.5) > >     import sys >     from types import FunctionType > >     class MetaClass(type): >         def __init__(cls, name, bases, dct): >             ## converts instance methods to static methods autom

Re: Question about metaclass

2011-11-01 Thread Ian Kelly
On Tue, Nov 1, 2011 at 10:02 PM, Makoto Kuwata wrote: > I tried the following code which converts instance mthods into > static method automatically, but I don't get result what I want. > (python 2.5.5) > >    import sys >    from types import FunctionType >

Question about metaclass

2011-11-01 Thread Makoto Kuwata
) Geeting.hello():#=> "Hello!" Geeting.goodbye(): #=> "Good Bye!" I tried the following code which converts instance mthods into static method automatically, but I don't get result what I want. (python 2.5.5) import sys from types import F

Re: questions (& answers) about object, type, builtin types, class, metaclass and __getattribute__

2011-08-23 Thread Amirouche B.
is circular and a bit confusing. These > fundamental objects are created special. The code snippet is here to illustrate how it is visible in the interpreter. But you are right. > > 2) type is its own metaclass : type(type) is type ? > > Only in a purely theoretical way. It doesn&

Re: questions (& answers) about object, type, builtin types, class, metaclass and __getattribute__

2011-08-23 Thread Amirouche B.
Type in PyObject that part is not hard. > > 2) type is its own metaclass : type(type) is type ? > > Yes. Another bit of bootstrapping that the compiler does. self reference is easy same as referencing PyType from PyObject and PyObject from PyType. > > 5) type(any_object) == last_me

Re: questions (& answers) about object, type, builtin types, class, metaclass and __getattribute__

2011-08-22 Thread Chris Angelico
On Mon, Aug 22, 2011 at 4:41 PM, Stephen Hansen wrote: > Not exactly. Python has two somewhat different object models, "old style > classes" and "new style classes", with slightly different behavior and > internal structure. > >   class Foo: pass > > is an "old-style class", dated back to Python's

Re: questions (& answers) about object, type, builtin types, class, metaclass and __getattribute__

2011-08-22 Thread Stephen Hansen
e at it in the interpreter, but it doesn't really /mean/ anything because its not how these objects came to be and is circular and a bit confusing. These fundamental objects are created special. > B) type vs metaclass > > > 1) type is the first metaclass ?

Re: questions (& answers) about object, type, builtin types, class, metaclass and __getattribute__

2011-08-22 Thread Steven D'Aprano
emselves objects, and therefore are instances of object: >>> isinstance(K, object) True Since classes are objects, they have a type, namely ``type``. This includes ``type`` itself: * type is an instance of object * object is an instance of type * type is a subclass of object

questions (& answers) about object, type, builtin types, class, metaclass and __getattribute__

2011-08-22 Thread Amirouche B.
ct.__class__ is type 4) type parent object is object : type.__bases__ == (object,) B) type vs metaclass ---- 1) type is the first metaclass ? 2) type is its own metaclass : type(type) is type ? 3) object's metaclass is type ? 4) other metaclasses *MUST* inherit type ? 5) type(any

Re: Calling super() in __init__ of a metaclass

2011-08-06 Thread Eli Bendersky
On Sat, Aug 6, 2011 at 11:04, Chris Rebert wrote: > On Sat, Aug 6, 2011 at 12:34 AM, Eli Bendersky wrote: >> Consider this standard metaclass definition: >> >> class MyMetaclass(type): >>    def __init__(cls, name, bases, dct): >>        super(MyMetaclas

Re: Calling super() in __init__ of a metaclass

2011-08-06 Thread Peter Otten
Eli Bendersky wrote: > Consider this standard metaclass definition: > > class MyMetaclass(type): > def __init__(cls, name, bases, dct): > super(MyMetaclass, cls).__init__(name, bases, dct) > # do meta-stuff > > class Foo(object): > __metacla

Re: Calling super() in __init__ of a metaclass

2011-08-06 Thread Chris Rebert
On Sat, Aug 6, 2011 at 12:34 AM, Eli Bendersky wrote: > Consider this standard metaclass definition: > > class MyMetaclass(type): >    def __init__(cls, name, bases, dct): >        super(MyMetaclass, cls).__init__(name, bases, dct) >        # do meta-stuff &g

Calling super() in __init__ of a metaclass

2011-08-06 Thread Eli Bendersky
Consider this standard metaclass definition: class MyMetaclass(type): def __init__(cls, name, bases, dct): super(MyMetaclass, cls).__init__(name, bases, dct) # do meta-stuff class Foo(object): __metaclass__ = MyMetaclass The call "super(MyMetaclass, cls)" shou

Re: Recursion error in metaclass

2011-06-11 Thread Daniel Urban
On Sat, Jun 11, 2011 at 21:39, Terry Reedy wrote: > What may not be obvious from the docs is that the metaclass calculation > described in the doc section on class statements is carried out within > type.__new__ (or after a possible patch, called from within that), so that > type call

Re: Recursion error in metaclass

2011-06-11 Thread Terry Reedy
On 6/11/2011 7:38 AM, Steven D'Aprano wrote: On Sat, 11 Jun 2011 01:33:25 -0400, Terry Reedy wrote: On 6/10/2011 11:34 PM, Steven D'Aprano wrote: I have a metaclass in Python 3.1: class MC1(type): @staticmethod def get_mro(bases): print('

Re: Recursion error in metaclass

2011-06-11 Thread Steven D'Aprano
On Sat, 11 Jun 2011 01:33:25 -0400, Terry Reedy wrote: > On 6/10/2011 11:34 PM, Steven D'Aprano wrote: >> I have a metaclass in Python 3.1: >> >> class MC1(type): >> @staticmethod >> def get_mro(bases): >> print('get_mro called&

Re: Recursion error in metaclass

2011-06-10 Thread Terry Reedy
On 6/10/2011 11:34 PM, Steven D'Aprano wrote: I have a metaclass in Python 3.1: class MC1(type): @staticmethod def get_mro(bases): print('get_mro called') return type('K', bases, {}).__mro__[1:] The call to type figures out the proper

Recursion error in metaclass

2011-06-10 Thread Steven D'Aprano
I have a metaclass in Python 3.1: class MC1(type): @staticmethod def get_mro(bases): print('get_mro called') return type('K', bases, {}).__mro__[1:] def __new__(cls, name, bases, dict): mro = None docstring = dict.get('__doc

Re: Python metaclass and UML

2011-01-31 Thread James Mills
On Tue, Feb 1, 2011 at 6:04 AM, Laszlo Nagy wrote: > How should I represent a Python metaclass on an UML class diagram? I know > how to represent composition, aggregation and inheritance. But not sure > about metaclasses. What kind of arrow or line should I use between a class > and i

Re: Python metaclass and UML

2011-01-31 Thread Ian Kelly
On Mon, Jan 31, 2011 at 1:04 PM, Laszlo Nagy wrote: > How should I represent a Python metaclass on an UML class diagram? I know > how to represent composition, aggregation and inheritance. But not sure > about metaclasses. What kind of arrow or line should I use between a class

Python metaclass and UML

2011-01-31 Thread Laszlo Nagy
How should I represent a Python metaclass on an UML class diagram? I know how to represent composition, aggregation and inheritance. But not sure about metaclasses. What kind of arrow or line should I use between a class and its metaclass? Is there a standard for this? Thanks, Laszlo

Re: abstract metaclass

2010-08-06 Thread Gabriel Genellina
En Thu, 05 Aug 2010 10:46:29 -0300, Roald de Vries escribió: I'm trying to create a metaclass that keeps track of its objects, and implement this as a collections.MutableMapping. That is, something like this: class type2(type, MutableMapping): ... /opt/local/Library/Frame

abstract metaclass

2010-08-05 Thread Roald de Vries
Hi all, I'm trying to create a metaclass that keeps track of its objects, and implement this as a collections.MutableMapping. That is, something like this: class type2(type, MutableMapping): ... /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/abc.p

Re: Inheritable Slots Metaclass

2010-05-31 Thread Aahz
In article <5a3a5737-f1b7-4419-9bb3-088c244a4...@c13g2000vbr.googlegroups.com>, Carl Banks wrote: > >However, Aahz will be by shortly to tell you never to use slots. Please note that there is an important distinction between "don't use slots" and "never use slots" -- if you can locate any instan

Re: Inheritable Slots Metaclass

2010-05-26 Thread Carl Banks
On May 26, 9:55 pm, Carl Banks wrote: > I don't want to sound to pessimistic about it, I really wouldn't mind > a metaclass that makes slots more normal; but you have work to do. Just as a minor followup, I'll mention that slots and inheritance have some issues th

  1   2   3   >