我正在尝试理解Python的变量范围方法.在这个例子中,为什么f()能够改变x内部感知main()的价值,而不是价值n?
def f(n, x):
n = 2
x.append(4)
print('In f():', n, x)
def main():
n = 1
x = [0,1,2,3]
print('Before:', n, x)
f(n, x)
print('After: ', n, x)
main()
Run Code Online (Sandbox Code Playgroud)
输出:
Before: 1 [0, 1, 2, 3]
In f(): 2 [0, 1, 2, 3, 4]
After: 1 [0, 1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud) 在ruby中快速生成长字符串的最佳方法是什么?这有效,但速度很慢:
str = ""
length = 100000
(1..length).each {|i| str += "0"}
Run Code Online (Sandbox Code Playgroud)
我还注意到,创建一个相当长的字符串,然后将其附加到现有字符串,达到所需的长度会更快:
str = ""
incrementor = ""
length = 100000
(1..1000).each {|i| incrementor += "0"}
(1..100).each {|i| str += incrementor}
Run Code Online (Sandbox Code Playgroud)
还有其他建议吗?
我正在尝试使用mysqldump仅导出数据库模式 - 没有数据,没有其他SQL注释,只有CREATE TABLE命令.这是我到目前为止所得到的:
mysqldump -h localhost -u root -p --no-data --compact some_db
Run Code Online (Sandbox Code Playgroud)
它几乎实现了我想要的,但我想消除"字符集"行(如下面示例输出中的前3行).有没有mysqldump选择呢?
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `foo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bar_id` int(11) DEFAULT NULL,
`bazz` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=369348 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 …Run Code Online (Sandbox Code Playgroud) 而不是每次我定义一个类时都编写这样的代码:
class Foo(object):
def __init__(self, a, b, c, d, e, f, g):
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
self.f = f
self.g = g
Run Code Online (Sandbox Code Playgroud)
我可以使用此配方进行自动属性分配.
class Foo(object):
@autoassign
def __init__(self, a, b, c, d, e, f, g):
pass
Run Code Online (Sandbox Code Playgroud)
两个问题:
我认为Python没有Perl的直接等价物,我是否正确__END__?
print "Perl...\n";
__END__
End of code. I can put anything I want here.
Run Code Online (Sandbox Code Playgroud)
我想到的一个想法是使用三引号字符串.有没有更好的方法在Python中实现这一点?
print "Python..."
"""
End of code. I can put anything I want here.
"""
Run Code Online (Sandbox Code Playgroud) 该List::MoreUtils模块表示您使用变量,$a并$b在提供该变量时BLOCK使用该pairwise功能.例如:
use strict;
use warnings;
use List::MoreUtils qw'pairwise';
my @x = ( 1 .. 5);
my @y = (11 .. 15);
my @sums = pairwise { $a + $b } @x, @y;
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,我得到这样的警告:
Name "main::b" used only once: possible typo at try.pl line 7. Name "main::a" used only once: possible typo at try.pl line 7.
有一种优雅的方式来处理这个问题吗?
更新:
如果你有一个属性需要在它设置的任何时候进行修改,那么是否有一种简单的方法可以自己编写访问器并直接使用内容$self,如本例中所做的那样?
package Foo;
use Moose;
has 'bar' => (
isa => 'Str',
reader => 'get_bar',
);
sub set_bar {
my ($self, $bar) = @_;
$self->{bar} = "modified: $bar";
}
Run Code Online (Sandbox Code Playgroud)
我考虑过trigger,但似乎需要采用相同的方法.
$self在Moose中被认为是不好的做法直接使用哈希引用,还是我担心没有问题?
该文档的qr/STRING/说:
此运算符引用(并可能编译)它
STRING作为正则表达式.
令我担心的是括号中的部分.我想不出任何我不希望它编译正则表达式的情况STRING.这个括号语句是否只是狡猾的话来涵盖一些未来需要编译的情况,或者今天(或者在Perl的早期版本中)STRING是否存在无法编译的情况?
我读过几次unpack()比这更快substr(),特别是随着子串数量的增加.但是,这个基准建议不然.我的基准测试是否存在缺陷,或者是unpack()旧版Perl的保留所谓的性能优势?
use strict;
use warnings;
use Benchmark;
my ($data, $format_string, $n_substrings);
my %methods = (
unpack => sub { return unpack $format_string, $data },
substr => sub { return map {substr $data, $_, 1} 0 .. $n_substrings - 1 },
);
for my $exp (1 .. 5){
$n_substrings = 10 ** $exp;
print $n_substrings, "\n";
$format_string = 'a1' x $n_substrings;
$data = 9 x $n_substrings;
Benchmark::cmpthese -2, \%methods;
}
Run Code Online (Sandbox Code Playgroud)
输出(在Windows上):
10
Rate unpack substr …Run Code Online (Sandbox Code Playgroud) 这里发生了什么?两种形式"除非"之间的细微差别是什么?
> irb(main):001:0> foo = true unless defined?(foo)
=> nil
irb(main):002:0> unless defined?(fooo) ; fooo = false ; end
=> false
Run Code Online (Sandbox Code Playgroud)
谢谢