为什么我要使用inspect.isclass()? [英] Why would I use inspect.isclass()?

查看:538
本文介绍了为什么我要使用inspect.isclass()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了inspect.isclass()正在做的事情。考虑这个没有

布莱纳码:


###

导入检查


班Abc:

def你好(个体经营):

返回


an_instance = Abc()

print inspect.isclass(an_instance)

###


我花了一段时间才明白如何获得inspect.isclass返回

True(如:inspect.isclass(Abc))。


但这有什么用呢?当然我知道Abc是一个班级,为什么我想要b $ b b检查它以便告诉我我已经知道的东西?


我会有以为这就是我需要的情况:


###

进口检查


class Abc :

def你好(self,some_class):

#因为我不知道传递的论点是*是*是否是一个班级,我
会:

如果inspect.isclass(s​​ome_class)==真:

...

返回

###


但这显然不是什么类。我该怎么用?


谢谢,

I discovered the hardway what inspect.isclass() is doing. Consider this no
brainer code:

###
import inspect

class Abc:
def Hello(self):
return

an_instance=Abc()
print inspect.isclass(an_instance)
###

It took me a while to understand how I can get inspect.isclass to return a
True (like: inspect.isclass(Abc)).

But what good is that? Of course I know Abc is a class, why would I want to
inspect it so that it would tell me what I already know?

I would have thought this would be the situation I need that for:

###
import inspect

class Abc:
def Hello(self, some_class):
# since I don''t know if the argument passed down *is* a class or not, I
would:
if inspect.isclass(some_class)==True:
...
return
###

But that obviously isn''t what isclass is for. What should I use?

Thanks,

推荐答案

> an_instance = Abc()
> an_instance=Abc()
但这有什么用呢?当然我知道Abc是一个班级,为什么我要检查它以便告诉我我已经知道的内容?


好​​吧,在那种情况下没有理由。出于同样的原因,如果你已经知道an_instance是一个abc的

实例,那么你就不会打电话给你实例(an_instance,abc)。

def你好(self,some_class):
#因为我不知道传递的论点是*是*是否是一个类,我会
如果inspect.isclass(s​​ome_class) ==正确:
...
返回
###

但这显然不是什么类。我应该使用什么?
But what good is that? Of course I know Abc is a class, why would I want to
inspect it so that it would tell me what I already know?
Well, for no reason in that case. For the same reason you would not
call isinstance(an_instance, Abc) if you already know an_instance is an
instance of Abc.
def Hello(self, some_class):
# since I don''t know if the argument passed down *is* a class or not, I
would:
if inspect.isclass(some_class)==True:
...
return
###

But that obviously isn''t what isclass is for. What should I use?




嗯,这显然是什么类的;)(顺便说一句,你会是

最好不要将你的条件与True进行比较,除非它真的是你想要的b $ b。


我想另一个例子是类型的断言争论:

def foo(someClass):

断言inspect.isclass(s​​omeClass)

#其余代码


这样,类型上的错误在开始时处理,并且在它自己记录的代码时间同时处理。该函数也可能有用

在你做一些C ++的情况下 - 比如重载机制。无论如何,

isclass,就像iscallable一样,是经常不常用的函数,但是你还是可能需要它们。


此致,

Nicolas



Well, it''s obviously what isclass is for;) (By the way, you would be
better not compare your conditions with True, unless it''s really what
you want).

I guess another example would be an assert on the type of argument:
def foo(someClass):
assert inspect.isclass(someClass)
# rest of code

This way errors on types are handled at the beginning and at the same
time the code it documenting itself. The function could also be useful
in cases where you do some C++-like overloading mechanism. Anyway,
isclass, like iscallable, are functions that are not used often, but you
still might need them.

Regards,
Nicolas


Nicolas,


感谢您的回复。请参阅下面的评论。


" Nicolas Fleury" < NI ****** @ yahoo.com_removethe_>在消息中写道

news:as ******************** @ weber.videotron.net ...
Nicolas,

Thanks for the response. Please see comment below.

"Nicolas Fleury" <ni******@yahoo.com_removethe_> wrote in message
news:as********************@weber.videotron.net...
an_instance = Abc()
an_instance=Abc()
但这有什么用呢?当然我知道Abc是一个班级,为什么我要b $ b想要检查它以便告诉我我已经知道的东西?
But what good is that? Of course I know Abc is a class, why would I want to inspect it so that it would tell me what I already know?



好吧,没有理由案件。出于同样的原因,如果你已经知道an_instance是Abc的实例,你就不会调用isinstance(an_instance,abc)。



Well, for no reason in that case. For the same reason you would not
call isinstance(an_instance, Abc) if you already know an_instance is an
instance of Abc.

def你好(自我, some_class):
#因为我不知道传递的参数是*是*类还是
不是,我会:
如果inspect.isclass(s​​ome_class)== True:
...
返回
###

但这显然不是什么类。我应该使用什么?
def Hello(self, some_class):
# since I don''t know if the argument passed down *is* a class or not, I would:
if inspect.isclass(some_class)==True:
...
return
###

But that obviously isn''t what isclass is for. What should I use?



嗯,这显然是isclass的用途;)(顺便说一下,你最好不要将你的条件与True比较,除非它真的是你想要的。)



Well, it''s obviously what isclass is for;) (By the way, you would be
better not compare your conditions with True, unless it''s really what
you want).




但是,无论如何,inspect.isclass(s​​ome_class)都会返回False。这就是为什么

我很困惑。


我得到isclass返回True的唯一方法是在我的

例子。


我想另一个例子是关于参数类型的断言:
def foo(someClass):
断言检查。 isclass(s​​omeClass)
#其余代码


但总会失败! (我只是试过了。)

这样,类型上的错误在开始时处理,并且在它自己编写代码的同一时间处理。在你做一些C ++的情况下,这个函数也很有用 - 比如重载机制。无论如何,
isclass,就像iscallable一样,是经常不常用的函数,但是你仍然可能需要它们。

问候,
Nicolas



But inspect.isclass(some_class) returns a False no matter what. That''s why
I am confused.

The only way I get isclass to return True is inspect.isclass(Abc) in my
example.

I guess another example would be an assert on the type of argument:
def foo(someClass):
assert inspect.isclass(someClass)
# rest of code

But that would always fail! (I just tried it).
This way errors on types are handled at the beginning and at the same
time the code it documenting itself. The function could also be useful
in cases where you do some C++-like overloading mechanism. Anyway,
isclass, like iscallable, are functions that are not used often, but you
still might need them.

Regards,
Nicolas



这是我写的:
我想另一个例子是关于参数类型的断言:
def foo(someClass):断言inspect.isclass(s​​omeClass)
#rest of code
I guess another example would be an assert on the type of argument:
def foo(someClass):
assert inspect.isclass(someClass)
# rest of code


但总会失败! (我刚试过它。)


But that would always fail! (I just tried it).




你确定你没有通过实例而不是课程吗?请记住,

类也是Python中的对象:



Are you sure you haven''t pass an instance instead of a class? Remember,
classes are also objects in Python:

import inspect
A类:传递
.... def foo(a):断言inspect.isclass(a)
.... foo(A)


工作正常,有道理,不是吗?当然,如果我传递一个实例

而不是一个类它会失败:

myA = A()
foo(myA)
import inspect
class A: pass .... def foo(a): assert inspect.isclass(a) .... foo(A)

Works fine and makes sense, no? Of course, if I pass an instance
instead of a class it would fail:
myA = A()
foo(myA)



回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

文件"< stdin>",第1行,在foo中

AssertionError


也许您的问题是在什么情况下通过课程可能有用吗?

有很多例子,一旦你习惯了这种能力,它就可以实现强大的功能。一个简单的例子就是生成

文档;因为你通常想要你的课程,而不是
实例。所以做一些事情是有道理的:


def writeHtmlDoc(file,someClass):...


问候,

Nicolas


Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in foo
AssertionError

Maybe your question is in what situations passing a class can be useful?
There''s many examples and once you''re used to that capability it can
be powerful. One simple example could be the generation of
documentation; since you usually want to doc your classes, not
instances. So it would make sense to do something like:

def writeHtmlDoc(file, someClass): ...

Regards,
Nicolas


这篇关于为什么我要使用inspect.isclass()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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