Perl 6 中 __init__ 的等效方法是什么?
what's the equivalent method of __init__ in Perl 6?
在Python中,__init__
用来初始化一个class:
class Auth(object):
def __init__(self, oauth_consumer, oauth_token=None, callback=None):
self.oauth_consumer = oauth_consumer
self.oauth_token = oauth_token or {}
self.callback = callback or 'http://localhost:8080/callback'
def HMAC_SHA1():
pass
Perl 6 中 init 的等效方法是什么?方法是new
?
在 Perl 6 中,每个 class 都有默认的 new
构造函数,您可以使用它来初始化对象的属性:
class Auth {
has $.oauth_consumer is required;
has $.oauth_token = {} ;
has $.callback = 'http://localhost:8080/callback';
method HMAC_SHA1() { say 'pass' }
}
my $auth = Auth.new( oauth_consumer => 'foo');
say "Calling method HMAC_SHA1:";
$auth.HMAC_SHA1;
say "Data dump of object $auth:";
dd $auth;
给出以下输出:
Calling method HMAC_SHA1:
pass
Data dump of object Auth<53461832>:
Auth $auth = Auth.new(oauth_consumer => "foo", oauth_token => ${}, callback => "http://localhost:8080/callback")
我建议查看 Håkon Hægland 在您的问题的评论中提到的 Class and Object tutorial and the page on Object Orientation (the latter page includes the Object Construction section。
为了迂腐,最接近的语法等价物是创建一个 submethod BUILD
(或 TWEAK
)。
这是最接近的翻译:
class Auth {
has $.oauth_consumer;
has $.oauth_token;
has $.callback;
submethod BUILD ( \oauth_consumer, \oauth_token=Nil, \callback=Nil ) {
$!oauth_consumer = oauth_consumer;
$!oauth_token = oauth_token // {};
$!callback = callback // 'http://localhost:8080/callback';
}
method HMAC_SHA1 ( --> 'pass' ) {}
}
这个比较地道
class Auth {
has $.oauth_consumer;
has $.oauth_token;
has $.callback;
submethod BUILD (
$!oauth_consumer,
$!oauth_token = {},
$!callback = 'http://localhost:8080/callback',
) {
# empty submethod
}
method HMAC_SHA1 ( --> 'pass' ) {}
}
为了真正地道,我会写克里斯托弗所做的。
Christopher Bottoms 和 Brad Gilbert 的回答是正确的。但是,我想指出一些可能使理解 Python 和 Perl6 之间的等价关系更容易的事情。
首先,this page about going from Python to Perl6 is full with them, including this section on classes and objects。
请注意,在 Perl6 中等同于 __init__
的是...无。构造函数是从实例变量自动生成的,包括默认值。然而,调用构造函数只需要 Python 中的 class 的名称,而在 Perl 6 中它使用 new
。
另一方面,there are many different ways of overriding that default behavior, from defining your own new
to using BUILD
, BUILDALL
or, even better, TWEAK
(通常定义为submethods
,因此不会被子class继承)。
最后,您确实可以 self
在 Perl 6 方法中引用对象本身。但是,我们通常会这样看(如上面的示例)self.instance-variable
→ $!instance-variable
(请注意 -
可以是 Perl 6 中标识符的有效部分)。
在Python中,__init__
用来初始化一个class:
class Auth(object):
def __init__(self, oauth_consumer, oauth_token=None, callback=None):
self.oauth_consumer = oauth_consumer
self.oauth_token = oauth_token or {}
self.callback = callback or 'http://localhost:8080/callback'
def HMAC_SHA1():
pass
Perl 6 中 init 的等效方法是什么?方法是new
?
在 Perl 6 中,每个 class 都有默认的 new
构造函数,您可以使用它来初始化对象的属性:
class Auth {
has $.oauth_consumer is required;
has $.oauth_token = {} ;
has $.callback = 'http://localhost:8080/callback';
method HMAC_SHA1() { say 'pass' }
}
my $auth = Auth.new( oauth_consumer => 'foo');
say "Calling method HMAC_SHA1:";
$auth.HMAC_SHA1;
say "Data dump of object $auth:";
dd $auth;
给出以下输出:
Calling method HMAC_SHA1: pass Data dump of object Auth<53461832>: Auth $auth = Auth.new(oauth_consumer => "foo", oauth_token => ${}, callback => "http://localhost:8080/callback")
我建议查看 Håkon Hægland 在您的问题的评论中提到的 Class and Object tutorial and the page on Object Orientation (the latter page includes the Object Construction section。
为了迂腐,最接近的语法等价物是创建一个 submethod BUILD
(或 TWEAK
)。
这是最接近的翻译:
class Auth {
has $.oauth_consumer;
has $.oauth_token;
has $.callback;
submethod BUILD ( \oauth_consumer, \oauth_token=Nil, \callback=Nil ) {
$!oauth_consumer = oauth_consumer;
$!oauth_token = oauth_token // {};
$!callback = callback // 'http://localhost:8080/callback';
}
method HMAC_SHA1 ( --> 'pass' ) {}
}
这个比较地道
class Auth {
has $.oauth_consumer;
has $.oauth_token;
has $.callback;
submethod BUILD (
$!oauth_consumer,
$!oauth_token = {},
$!callback = 'http://localhost:8080/callback',
) {
# empty submethod
}
method HMAC_SHA1 ( --> 'pass' ) {}
}
为了真正地道,我会写克里斯托弗所做的。
Christopher Bottoms 和 Brad Gilbert 的回答是正确的。但是,我想指出一些可能使理解 Python 和 Perl6 之间的等价关系更容易的事情。 首先,this page about going from Python to Perl6 is full with them, including this section on classes and objects。
请注意,在 Perl6 中等同于 __init__
的是...无。构造函数是从实例变量自动生成的,包括默认值。然而,调用构造函数只需要 Python 中的 class 的名称,而在 Perl 6 中它使用 new
。
另一方面,there are many different ways of overriding that default behavior, from defining your own new
to using BUILD
, BUILDALL
or, even better, TWEAK
(通常定义为submethods
,因此不会被子class继承)。
最后,您确实可以 self
在 Perl 6 方法中引用对象本身。但是,我们通常会这样看(如上面的示例)self.instance-variable
→ $!instance-variable
(请注意 -
可以是 Perl 6 中标识符的有效部分)。