识别新的未继承的方法 [英] identifying new not inherited methods

查看:42
本文介绍了识别新的未继承的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在编写一个库,我需要在其中找到在类中实现的方法名称

,而不是继承来自另一个

级。为了解释更多,并找出是否有另一种方式来做

,这就是我想要做的:我定义了两个类,比如A和B,

as:


A级(对象):

def list_cmds(self):

''需要实现''



def __init __(个体经营):

...(课堂其他)


B级(A):

def cmd1(自我,args):

通过

def cmd2(self,args):

通过


我需要在上面的A中实现list_cmds,这样我才能得到一个

结果:


>> b = B()
b.list_cmds()



[''cmd1'',''cmd2''] #order不重要


我会很开心如果有人能指点我做任何方式,使用

类属性,元类或其他伊势。我不想做的是

修改B类,如果可能的话只包含cmds。


非常感谢提前。


k

解决方案

malkaro ... @ gmail.com写道:
< blockquote class =post_quotes>



我正在写一个库,我需要在其中找到方法的名称

在一个类中实现,而不是从另一个继承的

类。为了解释更多,并找出是否有另一种方式来做

,这就是我想要做的:我定义了两个类,比如A和B,

as:


A级(对象):

def list_cmds(self):

''需要实现''



def __init __(个体经营):

...(课堂其他)


B级(A):

def cmd1(自我,args):

通过

def cmd2(self,args):

通过


我需要在上面的A中实现list_cmds,这样我才能得到一个

结果:


> b = B()
b.list_cmds()



[''cmd1'',''cmd2''] #order不重要


如果有人能指点我做任何方式我会很高兴,使用

类属性,元类或其他。我不想做的是

修改B类,如果可能的话只包含cmds。


非常感谢提前。


k



我宁愿将它作为一个函数,而不是附加到特定的类:


来自inspect import getmembers,ismethod


def listMethods(obj):

d = obj .__ class __.__ dict__

返回[姓名,_在getmembers(obj,ismethod)中如果名字在d]


HTH,

George


ma ******** @ gmail.com 写道:


我正在编写一个库,我需要找到在类中实现的方法名称

,而不是继承自另一个

类。为了解释更多,并找出是否有另一种方式来做

,这就是我想要做的:我定义了两个类,比如A和B,

as:


A级(对象):

def list_cmds(self):

''需要实现''



def __init __(个体经营):

...(课堂其他)


B级(A):

def cmd1(自我,args):

通过

def cmd2(self,args):

通过


我需要在上面的A中实现list_cmds,这样我才能得到一个

结果:


>>> b = B()
b.list_cmds()



[''cmd1'',''cmd2''] #order不重要


如果有人能指点我,我会很高兴它,使用

类属性,元类或o therwise。我不想做的是

修改B类,如果可能的话,它只包含cmds。



假设你想要所有

类中的* all *方法以cmd开头(包括A类中的任何命令) ),你可以做到


def list_cmds(个体经营):

#获取所有名字

names = dir(self)

#过滤命令

names = [名称中名称的名称,如果name.startswith(" cmd")]

返回名称<如果命令方法可以有任意名称,将测试改为

过滤掉你不感兴趣的方法,这通常是
。更容易

确保所有命令都使用通用名称前缀。


是的,如果你还没有这样做,请带一个看看cmd模块

在构建自己的变体之前:

http://effbot.org/librarybook/cmd.htm


希望这会有所帮助!


< ; / F>


我正在编写一个库,我需要在其中找到方法的名称


在一个类中实现,而不是从另一个

类继承。为了解释更多,并找出是否有另一种方式来做

,这就是我想要做的:我定义了两个类,比如A和B,

as:


A级(对象):

def list_cmds(self):

''需要实现''



def __init __(个体经营):

...(课堂其他)


B级(A):

def cmd1(自我,args):

通过

def cmd2(self,args):

通过


我需要在上面的A中实现list_cmds,这样我才能得到一个

结果:


>>> b = B()
b.list_cmds()



[''cmd1'',''cmd2''] #order不重要



虽然我不确定这是不是最好的/最pythonic的方式

它,但它做了m e:

########################################### ########## ###########

A类(对象):

def list_cmds(self):

root_cmds = set(dir(A))

child_cmds = set(dir(self))

返回列表(child_cmds - root_cmds)

def __init __(个体经营):

传递


B级(A):

def cmd1( self,args):

pass

def cmd2(self,args):

pass


b = B()

print repr(b.list_cmds())

#################### #########################################

如果你有多个继承,你只想要孩子添加的

方法,你可以试试像


####### ################################################## ####

A类(对象):

def list_cmds(self):

parent_cmds = set()

for self in self .__ class __.__ bases__:

parent_cmds.update(dir(base))

child_cmd s = set(dir(self))

返回列表(child_cmds - parent_cmds)

def __init __(个体经营):

传递


class C(对象):

def foo1(self,args):传递

def foo2(self,args):传递


B级(A,C):

def cmd1(自我,args):

通过

def cmd2(self,args):

pass


b = B()

print repr(b.list_cmds( ))


#################################### #########################

只是一些想法,


-tkc



Hi,

I am writing a library in which I need to find the names of methods
which are implemented in a class, rather than inherited from another
class. To explain more, and to find if there is another way of doing
it, here is what I want to do: I am defining two classes, say A and B,
as:

class A(object):
def list_cmds(self):
''implementation needed''
?
def __init__(self):
... (rest of class)

class B(A):
def cmd1(self, args):
pass
def cmd2(self, args):
pass

I need an implementation of list_cmds in A above so that I can get a
result:

>>b=B()
b.list_cmds()

[''cmd1'',''cmd2''] #order not important

I will be happy if anybody can point to me any way of doing it, using
class attributes, metaclasses or otherwise. What I don''t want to do is
modifying class B, which contains just the cmds, if possible.

Many thanks in advance.

k

解决方案

malkaro...@gmail.com wrote:

Hi,

I am writing a library in which I need to find the names of methods
which are implemented in a class, rather than inherited from another
class. To explain more, and to find if there is another way of doing
it, here is what I want to do: I am defining two classes, say A and B,
as:

class A(object):
def list_cmds(self):
''implementation needed''
?
def __init__(self):
... (rest of class)

class B(A):
def cmd1(self, args):
pass
def cmd2(self, args):
pass

I need an implementation of list_cmds in A above so that I can get a
result:

>b=B()
b.list_cmds()

[''cmd1'',''cmd2''] #order not important

I will be happy if anybody can point to me any way of doing it, using
class attributes, metaclasses or otherwise. What I don''t want to do is
modifying class B, which contains just the cmds, if possible.

Many thanks in advance.

k

I''d rather have it as a function, not attached to a specific class:

from inspect import getmembers, ismethod

def listMethods(obj):
d = obj.__class__.__dict__
return [name for name,_ in getmembers(obj,ismethod) if name in d]

HTH,
George


ma********@gmail.com wrote:

I am writing a library in which I need to find the names of methods
which are implemented in a class, rather than inherited from another
class. To explain more, and to find if there is another way of doing
it, here is what I want to do: I am defining two classes, say A and B,
as:

class A(object):
def list_cmds(self):
''implementation needed''
?
def __init__(self):
... (rest of class)

class B(A):
def cmd1(self, args):
pass
def cmd2(self, args):
pass

I need an implementation of list_cmds in A above so that I can get a
result:

>>>b=B()
b.list_cmds()

[''cmd1'',''cmd2''] #order not important

I will be happy if anybody can point to me any way of doing it, using
class attributes, metaclasses or otherwise. What I don''t want to do is
modifying class B, which contains just the cmds, if possible.

assuming that you want *all* methods that starts with "cmd", from all
classes (including any commands in class A), you can do

def list_cmds(self):
# get all names
names = dir(self)
# filter out the commands
names = [name for name in names if name.startswith("cmd")]
return names

if the command methods can have any arbitrary names, change the test to
filter out the methods you''re not interested in. it''s usually easier to
make sure that all commands use a common name prefix, though.

and yes, if you haven''t done so already, take a look at the "cmd" module
before you build your own variant:

http://effbot.org/librarybook/cmd.htm

hope this helps!

</F>


I am writing a library in which I need to find the names of methods

which are implemented in a class, rather than inherited from another
class. To explain more, and to find if there is another way of doing
it, here is what I want to do: I am defining two classes, say A and B,
as:

class A(object):
def list_cmds(self):
''implementation needed''
?
def __init__(self):
... (rest of class)

class B(A):
def cmd1(self, args):
pass
def cmd2(self, args):
pass

I need an implementation of list_cmds in A above so that I can get a
result:

>>>b=B()
b.list_cmds()

[''cmd1'',''cmd2''] #order not important

While I''m not sure if this is the best/most-pythonic way to do
it, but it did it for me:
################################################## ###########
class A(object):
def list_cmds(self):
root_cmds = set(dir(A))
child_cmds = set(dir(self))
return list(child_cmds - root_cmds)
def __init__(self):
pass

class B(A):
def cmd1(self, args):
pass
def cmd2(self, args):
pass

b = B()
print repr(b.list_cmds())
################################################## ###########
If you have multiple inheritance going on, and you only want the
methods that the child adds, you might try something like

################################################## ###########
class A(object):
def list_cmds(self):
parent_cmds = set()
for base in self.__class__.__bases__:
parent_cmds.update(dir(base))
child_cmds = set(dir(self))
return list(child_cmds - parent_cmds)
def __init__(self):
pass

class C(object):
def foo1(self, args): pass
def foo2(self, args): pass

class B(A, C):
def cmd1(self, args):
pass
def cmd2(self, args):
pass

b = B()
print repr(b.list_cmds())

################################################## ###########

Just a few ideas,

-tkc



这篇关于识别新的未继承的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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