防止定义类方法 [英] Preventing class methods from being defined

查看:62
本文介绍了防止定义类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个奇怪的概念,我真的不知道如何实现,但

我怀疑可以通过描述符或元类以某种方式实现:


我想要一个类,当实例化时,只定义某些方法,如果
a global表示可以使用这些方法。所以我想要一些

喜欢:


全球允许

allow = [" foo"," bar"]


A级:

def foo():

...


def bar():

...


def baz():

...


A的任何实例只有a.foo()和a.bar()但没有a.baz()

,因为它不在允许列表中。


我希望这是有道理的。

不要问为什么我需要这么奇怪的动物,我就这样做。我只是

不知道怎么接近它。


A类应该有一些特殊的元类来阻止这些方法

来自现有?我应该覆盖__new__还是什么?那些

方法是否应该用特殊的属性子类包装来阻止访问

如果它们不在列表中?什么是低开销的方法,

工作简单?


提前谢谢,

-David


-

介绍:

平庸的星云。

解决方案

On Sun,2006年1月15日18:41:02 -0800,

David Hirschfield< da **** @ ilm.com>写道:

我想要一个类,当实例化时,只定义某些方法
如果全局表明可以使用这些方法。所以我想要
类似:
全球允许
允许= [" foo"," bar"]
A类:
def foo():
...
def bar():
...
def baz():
...
任何A的实例都只有一个.foo()和a.bar()但没有a.baz()
因为它不在允许列表中。
我希望这是有道理的。


我是这么认为的,至少在我可以实现这个想法中感觉,虽然

不是为什么你需要这么奇怪的动物?感。由于class

是Python中的可执行语句,所以应该这样做:


allow = [" foo"," bar" ]


A级:


如果foo允许:

def foo():

...


ifbar允许:

def bar():

...

不要问为什么我需要这么奇怪的动物.. 。




考虑自己不被问到。


HTH,

Dan
< br $> b $ b -

Dan Sommers

< http://www.tombstonezero.net/dan/>


我应该明确提到我不想要这个特别的

解决方案,原因有很多。

还有另一个使这项工作的方式,而不需要显示

明确的如果允许围绕每个方法定义?


再次感谢,

-David


Dan Sommers写道:

On Sun,2006年1月15日18:41:02 -0800,
David Hirschfield< da **** @ ilm.com>写道:

我想要一个类,当实例化时,只定义某些方法
如果全局表明可以使用这些方法。所以我想要
类似的东西:


global allow
allow = [" foo"," bar"]


A类:
def foo():
......


def bar():
...


def baz():
。 ..


A的任何实例只有a.foo()和a.bar()但没有a.baz()
因为它不在允许列表中。


我希望这是有道理的。



我是这么认为的,至少在我可以实现这个想法中感觉,虽然不是为什么你需要这么奇怪的动物?感。由于class
是Python中的可执行语句,所以应该这样做:

allow = [" foo"," bar"]

> A级:

如果foo允许:
def foo():
...

如果bar允许:
def bar():
...

不要问为什么我需要这么奇怪的动物......



考虑一下自己没被问到。

HTH,
Dan




-

演讲:

平庸的星云。


David Hirschfield< da ****@ilm.com>写道:

这是一个奇怪的概念,我真的不知道如何实现,但我怀疑可以通过描述符或元类以某种方式实现:


是的,一个自定义元类很容易做到。

我想要一个类,当实例化时,只定义某些方法,如果全局指示拥有这些方法是可以的。所以我想要一些像

全球允许
允许= [" foo"," bar"]

A类:
def foo():


def吧():


def baz():
...

A的任何实例只有a.foo()和a.bar()但没有a.baz()
因为它不是允许的清单。

我希望这是有道理的。




当然。如果你不需要担心继承,并且想要基于

值的'方法和其他类属性的'b $ b''快照''在类声明执行时'允许':


class meta_only_allowed(type):

def __new __(mcl,name,bases,cdict):
$ cd $ b for cd in cdict.keys():

如果k不允许:

del cdict [k]

返回super(mcl,meta_only_allowed).__ new __(

mcl,name,bases,cdict)


[[未经测试,但我希望这个概念是明确]]。


如果你想在class- * instantiation *时间''允许''的值在

控制中,那你呢需要覆盖是__call__而不是__new__。

你需要为此目的动态制作一个特殊的课程(制作

一定要记住它以避免不必要的重复)。


如果你确实需要担心继承,并且想要不允许

继承了方法,然后你需要遍历

基类中的所有方法(使用标准库模块检查)和

构造一个人工字典,然后放弃''基础''并使用一个空的
基元为超级调用(真正的继承不会让你

" hide"方法,或其他超类属性,永远)。


等等 - 最有可能做你想做的事,无论是什么

你想要的确实是! - )

Alex


Here''s a strange concept that I don''t really know how to implement, but
I suspect can be implemented via descriptors or metaclasses somehow:

I want a class that, when instantiated, only defines certain methods if
a global indicates it is okay to have those methods. So I want something
like:

global allow
allow = ["foo","bar"]

class A:
def foo():
...

def bar():
...

def baz():
...

any instance of A will only have a.foo() and a.bar() but no a.baz()
because it wasn''t in the allow list.

I hope that makes sense.
Don''t ask why I would need such a strange animal, I just do. I''m just
not sure how to approach it.

Should class A have some special metaclass that prevents those methods
from existing? Should I override __new__ or something? Should those
methods be wrapped with special property subclasses that prevent access
if they''re not in the list? What''s a low-overhead approach that will
work simply?

Thanks in advance,
-David

--
Presenting:
mediocre nebula.

解决方案

On Sun, 15 Jan 2006 18:41:02 -0800,
David Hirschfield <da****@ilm.com> wrote:

I want a class that, when instantiated, only defines certain methods
if a global indicates it is okay to have those methods. So I want
something like: global allow
allow = ["foo","bar"] class A:
def foo():
... def bar():
... def baz():
... any instance of A will only have a.foo() and a.bar() but no a.baz()
because it wasn''t in the allow list. I hope that makes sense.
I think so, at least in the "I can implement that idea" sense, although
not the "why would you need such a strange animal" sense. Since "class"
is an executable statement in Python, this ought to do it:

allow = ["foo", "bar"]

class A:

if "foo" in allow:
def foo( ):
...

if "bar" in allow:
def bar( ):
...
Don''t ask why I would need such a strange animal ...



Consider yourself not asked.

HTH,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>


I should have explicitly mentioned that I didn''t want this particular
solution, for a number of silly reasons.
Is there another way to make this work, without needing to place an
explicit "if allowed" around each method definition?

Thanks again,
-David

Dan Sommers wrote:

On Sun, 15 Jan 2006 18:41:02 -0800,
David Hirschfield <da****@ilm.com> wrote:

I want a class that, when instantiated, only defines certain methods
if a global indicates it is okay to have those methods. So I want
something like:


global allow
allow = ["foo","bar"]


class A:
def foo():
...


def bar():
...


def baz():
...


any instance of A will only have a.foo() and a.bar() but no a.baz()
because it wasn''t in the allow list.


I hope that makes sense.



I think so, at least in the "I can implement that idea" sense, although
not the "why would you need such a strange animal" sense. Since "class"
is an executable statement in Python, this ought to do it:

allow = ["foo", "bar"]

class A:

if "foo" in allow:
def foo( ):
...

if "bar" in allow:
def bar( ):
...

Don''t ask why I would need such a strange animal ...



Consider yourself not asked.

HTH,
Dan



--
Presenting:
mediocre nebula.


David Hirschfield <da****@ilm.com> wrote:

Here''s a strange concept that I don''t really know how to implement, but
I suspect can be implemented via descriptors or metaclasses somehow:
Yeah, a custom metaclass will do it, easily.
I want a class that, when instantiated, only defines certain methods if
a global indicates it is okay to have those methods. So I want something
like:

global allow
allow = ["foo","bar"]

class A:
def foo():
...

def bar():
...

def baz():
...

any instance of A will only have a.foo() and a.bar() but no a.baz()
because it wasn''t in the allow list.

I hope that makes sense.



Sure. If you don''t need to worry about inheritance, and want to
''snapshot'' the set of methods and other class attributes based on the
value of ''allow'' at the time the class statement executes:

class meta_only_allowed(type):
def __new__(mcl, name, bases, cdict):
for k in cdict.keys():
if k not in allow:
del cdict[k]
return super(mcl, meta_only_allowed).__new__(
mcl, name, bases, cdict)

[[untested, but I hope the concept is clear]].

If you want the value of ''allow'' at class-*instantiation* time to be in
control, then what you need to override is __call__ rather than __new__.
You''ll need to make a special class on the fly for the purpose (make
sure you memoize it to avoid needless duplication).

If you do need to worry about inheritance, and want to disallow
inherited methods as well, then you need to loop over all methods in
base classes (use standard library module inspect for that) and
construct an artificial dict, then drop the ''bases'' and use an empty
tuple of bases for the supercall (real inheritance doesn''t let you
"hide" methods, or other superclass attributes, ever).

Etc, etc -- it''s most surely possible to do what you want, whatever what
you DO want is exactly!-)
Alex


这篇关于防止定义类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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