博格叛乱可能吗? (一个元类问题) [英] Is a Borg rebellion possible? (a metaclass question)

查看:50
本文介绍了博格叛乱可能吗? (一个元类问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用了由Alex发明的Borg成语

Martelli。


类Borg(对象):

''''''Borg Idiom,来自Python Cookbook,第2版,第273页


派出一个类表格;该类的所有实例将分享

相同的状态,前提是它们不会覆盖__new__;否则,

记得在覆盖类中使用Borg .__ new__。

'''''

_shared_state = {}

def __new __(cls,* a,** k):

obj = object .__ new __(cls,* a,** k)

obj .__ dict__ = cls._shared_state

返回obj


----

到目前为止这种方法运作良好,但是开始对我的程序设计施加一些不必要的约束。


我想做的是,比喻说,创建一个Borg
各种分裂组的
反叛。在具体的Python术语中,我想要
想要
类MyClass(Borg,...):

.. 。


seven_of_nine = MyClass(...)#Group of part" BORG"
two_of_nine = MyClass(...)


splinter1 = MyClass(...,group =''splinter'')

splinter2 = MyClass(...,group =''splinter'')


并且分裂1和splinter2共享相同的状态,但是与BORG集体成员共享的状态不同。

来自元类专家的任何建议?

$ b $bAndré

In my application, I make use of the Borg idiom, invented by Alex
Martelli.

class Borg(object):
''''''Borg Idiom, from the Python Cookbook, 2nd Edition, p:273

Derive a class form this; all instances of that class will share
the
same state, provided that they don''t override __new__; otherwise,
remember to use Borg.__new__ within the overriden class.
''''''
_shared_state = {}
def __new__(cls, *a, **k):
obj = object.__new__(cls, *a, **k)
obj.__dict__ = cls._shared_state
return obj

----
This has worked very well so far, but is starting to impose some
unwanted constraints on my program design.

What I would like to do is, to put it figuratively, create a Borg
rebellion with various splinter groups. In concrete Python terms, I
would like to have

class MyClass(Borg, ...):
...

seven_of_nine = MyClass(...) # part of group "BORG"
two_of_nine = MyClass(...)

splinter1 = MyClass(..., group=''splinter'')
splinter2 = MyClass(..., group=''splinter'')

and have splinter 1 and splinter2 share the same state, but a
different state than the one shared by members of the BORG collective.

Any suggestions from the metaclass experts?

André

推荐答案

André在周五,2007-09-07 12:31 +0000写道:
On Fri, 2007-09-07 at 12:31 +0000, André wrote:

在我的应用程序中,我使用了由Alex发明的Borg成语

Martelli。


类Borg(对象):

''''''Borg Idiom,来自Python Cookbook,第2版版,p:273

从此获得一个班级表格;该类的所有实例将分享

相同的状态,前提是它们不会覆盖__new__;否则,

记得在覆盖类中使用Borg .__ new__。

'''''

_shared_state = {}

def __new __(cls,* a,** k):

obj = object .__ new __(cls,* a,** k)

obj .__ dict__ = cls._shared_state

返回obj


----

到目前为止这种方法运作良好,但是开始对我的程序设计施加一些不必要的约束。


我想做的是,比喻说,创建一个Borg
各种分裂组的
反叛。在具体的Python术语中,我想要
想要
类MyClass(Borg,...):

.. 。


seven_of_nine = MyClass(...)#Group of part" BORG"
two_of_nine = MyClass(...)


splinter1 = MyClass(...,group =''splinter'')

splinter2 = MyClass(...,group =''splinter'')


并且分裂1和splinter2共享相同的状态,但是与BORG集体成员共享的状态不同。

元类专家的任何建议?
In my application, I make use of the Borg idiom, invented by Alex
Martelli.

class Borg(object):
''''''Borg Idiom, from the Python Cookbook, 2nd Edition, p:273

Derive a class form this; all instances of that class will share
the
same state, provided that they don''t override __new__; otherwise,
remember to use Borg.__new__ within the overriden class.
''''''
_shared_state = {}
def __new__(cls, *a, **k):
obj = object.__new__(cls, *a, **k)
obj.__dict__ = cls._shared_state
return obj

----
This has worked very well so far, but is starting to impose some
unwanted constraints on my program design.

What I would like to do is, to put it figuratively, create a Borg
rebellion with various splinter groups. In concrete Python terms, I
would like to have

class MyClass(Borg, ...):
...

seven_of_nine = MyClass(...) # part of group "BORG"
two_of_nine = MyClass(...)

splinter1 = MyClass(..., group=''splinter'')
splinter2 = MyClass(..., group=''splinter'')

and have splinter 1 and splinter2 share the same state, but a
different state than the one shared by members of the BORG collective.

Any suggestions from the metaclass experts?



你不需要元类。只需将_shared_state转换为

共享状态的字典,按组名称键入:


类SplinterBorg(对象):

_shared_states = {}

def __new __(cls,* a,** k):

group = k.pop(" group"," BORG")

obj = object .__ new __(cls,* a,** k)

obj .__ dict__ = cls._shared_states.setdefault(group,{})

返回obj


HTH,


-

Carsten Haese
http://informixdb.sourceforge.net


9月7日上午10点27分,Carsten Haese< cars ... @ uniqsys.comwrote:
On Sep 7, 10:27 am, Carsten Haese <cars...@uniqsys.comwrote:

周五,2007-09-07 12: 31 +0000,André写道:
On Fri, 2007-09-07 at 12:31 +0000, André wrote:

在我的应用程序中,我使用了由Alex发明的Borg成语

Martelli。
In my application, I make use of the Borg idiom, invented by Alex
Martelli.


class Borg(object):

''''''Borg Idiom,来自Python Cookbook,第2版,p:273
class Borg(object):
''''''Borg Idiom, from the Python Cookbook, 2nd Edition, p:273


派出一个类表格;该类的所有实例将分享

相同的状态,前提是它们不会覆盖__new__;否则,

记得在覆盖类中使用Borg .__ new__。

'''''

_shared_state = {}

def __new __(cls,* a,** k):

obj = object .__ new __(cls,* a,** k)

obj .__ dict__ = cls._shared_state

返回obj
Derive a class form this; all instances of that class will share
the
same state, provided that they don''t override __new__; otherwise,
remember to use Borg.__new__ within the overriden class.
''''''
_shared_state = {}
def __new__(cls, *a, **k):
obj = object.__new__(cls, *a, **k)
obj.__dict__ = cls._shared_state
return obj


----

这有到目前为止工作得很好,但是开始对我的程序设计施加一些不必要的限制。
----
This has worked very well so far, but is starting to impose some
unwanted constraints on my program design.


我想做的是,比喻说,用各种分裂组创建一个Borg

的反叛。在具体的Python术语中,我希望
想要
What I would like to do is, to put it figuratively, create a Borg
rebellion with various splinter groups. In concrete Python terms, I
would like to have


class MyClass(Borg,...):

...
class MyClass(Borg, ...):
...


seven_of_nine = MyClass(...)#part of group" BORG"

two_of_nine = MyClass(...)
seven_of_nine = MyClass(...) # part of group "BORG"
two_of_nine = MyClass(...)


splinter1 = MyClass(...,group =''splinter'')

splinter2 = MyClass(...,group =''splinter'')
splinter1 = MyClass(..., group=''splinter'')
splinter2 = MyClass(..., group=''splinter'')


并且有分裂1和splinter2共享相同州,但与BORG集体成员共享的州不同。
and have splinter 1 and splinter2 share the same state, but a
different state than the one shared by members of the BORG collective.


来自元类专家的任何建议?
Any suggestions from the metaclass experts?



你不需要元类。只需将_shared_state转换为

共享状态的字典,按组名称键入:


类SplinterBorg(对象):

_shared_states = {}

def __new __(cls,* a,** k):

group = k.pop(" group"," BORG")

obj = object .__ new __(cls,* a,** k)

obj .__ dict__ = cls._shared_states.setdefault(group,{})

返回obj


HTH,

-

Carsten Haesehttp://informixdb.sourceforge。 net


You don''t need a metaclass. Just turn _shared_state into a dictionary of
shared states, keyed by the group name:

class SplinterBorg(object):
_shared_states = {}
def __new__(cls, *a, **k):
group = k.pop("group","BORG")
obj = object.__new__(cls, *a, **k)
obj.__dict__ = cls._shared_states.setdefault(group,{})
return obj

HTH,

--
Carsten Haesehttp://informixdb.sourceforge.net



不幸的是,它失败了。这是我尝试过的,接着是

回溯

类SplinterBorg(对象):

_shared_states = {}

def __new __(cls,* a,** k):

group = k.pop(" group"," BORG")

obj = object .__ new __(cls,* a,** k)

obj .__ dict__ = cls._shared_states.setdefault(group,{})

return obj


class MyClass(SplinterBorg):

def __init __(self,name):

self.name = name


a1 = MyClass('''')

a2 = MyClass(''aa'')

b1 = MyClass(''b' ',group =" B")

Traceback(最近一次调用最后一次):

文件" test.py",第15行,< module>

b1 = MyClass(''b'',group =" B")

TypeError:__ init __()得到一个意外的关键字参数''group''

Unfortunately, it fails. Here''s what I tried, followed by the
traceback
class SplinterBorg(object):
_shared_states = {}
def __new__(cls, *a, **k):
group = k.pop("group","BORG")
obj = object.__new__(cls, *a, **k)
obj.__dict__ = cls._shared_states.setdefault(group,{})
return obj

class MyClass(SplinterBorg):
def __init__(self, name):
self.name = name

a1 = MyClass(''a'')
a2 = MyClass(''aa'')
b1 = MyClass(''b'', group="B")
Traceback (most recent call last):
File "test.py", line 15, in <module>
b1 = MyClass(''b'', group="B")
TypeError: __init__() got an unexpected keyword argument ''group''


André写道:
André wrote:

9月7日上午10:27,Ca rsten Haese< cars ... @ uniqsys.comwrote:
On Sep 7, 10:27 am, Carsten Haese <cars...@uniqsys.comwrote:

> On Fri,2007-09-07 at 12:31 +0000,André写道:
>On Fri, 2007-09-07 at 12:31 +0000, André wrote:

>>在我的应用程序中,我使用了由Alex发明的Borg成语
Martelli。
类Borg(对象):
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''该类的所有实例都将共享
相同的状态,前提是它们不会覆盖__new__;否则,
记得在重写类中使用Borg .__ new__。
'''''
_shared_state = {}
def __new __(cls,* a,** k ):
obj = object .__ new __(cls,* a,** k)
obj .__ dict__ = cls._shared_state
return obj
----
到目前为止,这一点非常顺利,但是我的程序设计开始受到一些不必要的限制。
我想做的是,比喻说,创造一个Borg
叛逆与各种分裂组。在具体的Python术语中,我想
类MyClass(Borg,...):
...
seven_of_nine = MyClass(...)#part of groupBORG
two_of_nine = MyClass(...)
splinter1 = MyClass(...,group =''splinter'')
splinter2 = MyClass(...,group ='''splinter'')
并且让splinter 1和splinter2共享相同的状态,但是与BORG集体成员共享的状态不同。
来自元类的任何建议专家?
>>In my application, I make use of the Borg idiom, invented by Alex
Martelli.
class Borg(object):
''''''Borg Idiom, from the Python Cookbook, 2nd Edition, p:273
Derive a class form this; all instances of that class will share
the
same state, provided that they don''t override __new__; otherwise,
remember to use Borg.__new__ within the overriden class.
''''''
_shared_state = {}
def __new__(cls, *a, **k):
obj = object.__new__(cls, *a, **k)
obj.__dict__ = cls._shared_state
return obj
----
This has worked very well so far, but is starting to impose some
unwanted constraints on my program design.
What I would like to do is, to put it figuratively, create a Borg
rebellion with various splinter groups. In concrete Python terms, I
would like to have
class MyClass(Borg, ...):
...
seven_of_nine = MyClass(...) # part of group "BORG"
two_of_nine = MyClass(...)
splinter1 = MyClass(..., group=''splinter'')
splinter2 = MyClass(..., group=''splinter'')
and have splinter 1 and splinter2 share the same state, but a
different state than the one shared by members of the BORG collective.
Any suggestions from the metaclass experts?


你不需要元类。只需将_shared_state转换为共享状态字典,按组名称键入:

类SplinterBorg(对象):
_shared_states = {}
def __new __(cls ,* a,** k):
group = k.pop(group,BORG)
obj = object .__ new __(cls,* a,** k)
obj .__ dict__ = cls._shared_states.setdefault(group,{})
返回obj

HTH,

-
Carsten Haesehttp: //informixdb.sourceforge.net

You don''t need a metaclass. Just turn _shared_state into a dictionary of
shared states, keyed by the group name:

class SplinterBorg(object):
_shared_states = {}
def __new__(cls, *a, **k):
group = k.pop("group","BORG")
obj = object.__new__(cls, *a, **k)
obj.__dict__ = cls._shared_states.setdefault(group,{})
return obj

HTH,

--
Carsten Haesehttp://informixdb.sourceforge.net



不幸的是,它失败了。这是我尝试过的,接着是

回溯

类SplinterBorg(对象):

_shared_states = {}

def __new __(cls,* a,** k):

group = k.pop(" group"," BORG")

obj = object .__ new __(cls,* a,** k)

obj .__ dict__ = cls._shared_states.setdefault(group,{})

return obj


class MyClass(SplinterBorg):

def __init __(self,name):

self.name = name


a1 = MyClass('''')

a2 = MyClass(''aa'')

b1 = MyClass(''b' ',group =" B")


Traceback(最近一次调用最后一次):

文件" test.py",第15行,< ;模块>

b1 = MyClass(''b'',group =" B")

TypeError:__ init __()得到一个意外的关键字参数''group' '


Unfortunately, it fails. Here''s what I tried, followed by the
traceback
class SplinterBorg(object):
_shared_states = {}
def __new__(cls, *a, **k):
group = k.pop("group","BORG")
obj = object.__new__(cls, *a, **k)
obj.__dict__ = cls._shared_states.setdefault(group,{})
return obj

class MyClass(SplinterBorg):
def __init__(self, name):
self.name = name

a1 = MyClass(''a'')
a2 = MyClass(''aa'')
b1 = MyClass(''b'', group="B")
Traceback (most recent call last):
File "test.py", line 15, in <module>
b1 = MyClass(''b'', group="B")
TypeError: __init__() got an unexpected keyword argument ''group''



因为你的子类是点燃是完全错误的。为什么你觉得你必须在你的子类中添加__init __()方法?
?这有两个bvad

效果:


1.它会阻止超类的__init __()方法被调用,并且


2.它打破了超类中指定的调用语法。


你真正需要的是用适当的创建你的SplinterBorgs

组名,你根本不是neef子类:


a1 = SplinterBorg(group =" one")

a2 = SplinterBorg(group =两个)


等等。


如果你想创建子类,那么它们应该像这样工作:


class MyBorg1(SplinterBorg):

def __init __(self):

SplinterBorg .__ init __(self,group =''borg1'' )


等等,每个都有不同的组。但这似乎过于复杂。


问候

Steve

-

Steve Holden +1 571 484 6266 +1 800 494 3119

Holden Web LLC / Ltd http:/ /www.holdenweb.com

Skype:holdenweb http://del.icio.us/steve.holden

--------------- Asciimercial -------- ----------

上网:博客,镜头和互联网标签

许多服务目前提供免费注册

-----------感谢您阅读-------------

Because your subclass signature is completely wrong. Why did you feel
you had to add an __init__() method to your subclass? This has two bvad
effects:

1. It stops the super-class''s __init__() method from being called, and

2. It breaks the calling syntax specified in the superclass.

All you really need is to create your SplinterBorgs with appropriate
group names, you don''t neef subclasses at all:

a1 = SplinterBorg(group="one")
a2 = SplinterBorg(group="two")

and so on.

If you want to create subclasses then they should work like this:

class MyBorg1(SplinterBorg):
def __init__(self):
SplinterBorg.__init__(self, group=''borg1'')

and so on, with a different group for each. But this seems over complicated.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------


这篇关于博格叛乱可能吗? (一个元类问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆