If I want to check whether a variable inherits from some class, I can either use is
or inherits
.
如果我想检查变量是否继承自某个类,我可以使用is或inherits。
class(letters)
## [1] "character"
is(letters, "character")
## [1] TRUE
inherits(letters, "character")
## [1] TRUE
Is there a preference for which one I should use, and do they ever return different values?
我是否应该选择使用哪一种,并且他们是否会返回不同的值?
1 个解决方案
#1
10
Short version:
简洁版本:
Use inherits
, but be careful with numbers and S4 classes.
使用继承,但要注意数字和S4类。
Longer version:
更长的版本:
From the See Also section of the is
help page:
从帮助页面的另请参阅部分:
inherits is nearly always equivalent to is, both for S4 and non-S4 objects, and is somewhat faster. The non-equivalence applies to classes that have conditional superclasses, with a non-trivial test= in the relation (not common and discouraged): for these, is tests for the relation but inherits by definition ignores conditional inheritance for S4 objects.
继承几乎总是等同于S4,对于S4和非S4对象,并且有点快。非等价适用于具有条件超类的类,在关系中具有非平凡的测试=(不常见且不鼓励):对于这些,是对关系的测试,但是根据定义继承忽略S4对象的条件继承。
From the Formal Classes section of the inherits
help page:
从继承帮助页面的Formal Classes部分:
The analogue of inherits for formal classes is is. The two functions behave consistently with one exception: S4 classes can have conditional inheritance, with an explicit test. In this case, is will test the condition, but inherits ignores all conditional superclasses.
正式类的继承类似物是。这两个函数的行为一致,但有一个例外:S4类可以具有条件继承,并具有显式测试。在这种情况下,将测试条件,但继承忽略所有条件超类。
So they mostly return the same thing, but inherits
is faster, so it should be the default choice in most cases. (As mentioned by Konrad, is
also requires that the methods
package is loaded, which may make it unsuitable for performance sensitive uses of Rscript
.)
所以他们大多返回相同的东西,但是继承更快,所以在大多数情况下它应该是默认选择。 (如Konrad所述,还要求加载方法包,这可能使其不适合Rscript的性能敏感用法。)
The values can differ if you are using S4 classes with conditional inheritance, but this is not recommended (see "Method Selection and Dispatch: Details" section), which means that it is hopefully rare.
如果您使用具有条件继承的S4类,则值可能不同,但不建议这样做(请参阅“方法选择和调度:详细信息”部分),这意味着它很少见。
The most obvious place where the two functions differ is when checking if integers are numeric.
两个函数不同的最明显的地方是检查整数是否为数字。
class(1L)
## [1] "integer"
is.numeric(1L)
## [1] TRUE
is(1L, "numeric")
## [1] TRUE
inherits(1L, "numeric")
## [1] FALSE
#1
10
Short version:
简洁版本:
Use inherits
, but be careful with numbers and S4 classes.
使用继承,但要注意数字和S4类。
Longer version:
更长的版本:
From the See Also section of the is
help page:
从帮助页面的另请参阅部分:
inherits is nearly always equivalent to is, both for S4 and non-S4 objects, and is somewhat faster. The non-equivalence applies to classes that have conditional superclasses, with a non-trivial test= in the relation (not common and discouraged): for these, is tests for the relation but inherits by definition ignores conditional inheritance for S4 objects.
继承几乎总是等同于S4,对于S4和非S4对象,并且有点快。非等价适用于具有条件超类的类,在关系中具有非平凡的测试=(不常见且不鼓励):对于这些,是对关系的测试,但是根据定义继承忽略S4对象的条件继承。
From the Formal Classes section of the inherits
help page:
从继承帮助页面的Formal Classes部分:
The analogue of inherits for formal classes is is. The two functions behave consistently with one exception: S4 classes can have conditional inheritance, with an explicit test. In this case, is will test the condition, but inherits ignores all conditional superclasses.
正式类的继承类似物是。这两个函数的行为一致,但有一个例外:S4类可以具有条件继承,并具有显式测试。在这种情况下,将测试条件,但继承忽略所有条件超类。
So they mostly return the same thing, but inherits
is faster, so it should be the default choice in most cases. (As mentioned by Konrad, is
also requires that the methods
package is loaded, which may make it unsuitable for performance sensitive uses of Rscript
.)
所以他们大多返回相同的东西,但是继承更快,所以在大多数情况下它应该是默认选择。 (如Konrad所述,还要求加载方法包,这可能使其不适合Rscript的性能敏感用法。)
The values can differ if you are using S4 classes with conditional inheritance, but this is not recommended (see "Method Selection and Dispatch: Details" section), which means that it is hopefully rare.
如果您使用具有条件继承的S4类,则值可能不同,但不建议这样做(请参阅“方法选择和调度:详细信息”部分),这意味着它很少见。
The most obvious place where the two functions differ is when checking if integers are numeric.
两个函数不同的最明显的地方是检查整数是否为数字。
class(1L)
## [1] "integer"
is.numeric(1L)
## [1] TRUE
is(1L, "numeric")
## [1] TRUE
inherits(1L, "numeric")
## [1] FALSE