You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/Language/py-nutshell.pod6
+60-27Lines changed: 60 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,9 +46,9 @@ contained in curly braces are interpolated.
46
46
Perl 6
47
47
48
48
my $planet = 'earth';
49
-
say "Hello, $planet"; # Hello, earth
50
-
say 'Hello, $planet'; # Hello, $planet
49
+
say "Hello, $planet"; # Hello, earth
51
50
say "Hello, planet number { 1 + 2 }"; # Hello, planet number 3
51
+
say 'I have $2'; # I have $2
52
52
53
53
=head2Statement Separators
54
54
@@ -64,39 +64,41 @@ The semicolon may be omitted if it is the last statement
64
64
of a block. The semicolon may also be omitted if there
65
65
is a closing curly brace followed by a newline.
66
66
67
-
Python:
67
+
Python
68
68
69
69
print 1 + 2 + \
70
70
3 + 4
71
71
print ( 1 +
72
72
2 )
73
73
74
-
Perl 6:
74
+
Perl 6
75
75
76
76
say 1 + 2 +
77
77
3 + 4;
78
78
if True { say 1 + 2 }
79
79
80
80
=head2Blocks
81
81
82
-
In Python indentation is used to indicate a block. Perl 6
82
+
In Python, indentation is used to indicate a block. Perl 6
83
83
uses curly braces.
84
84
85
-
Python:
85
+
Python
86
+
86
87
if 1 == 2:
87
88
print "Wait, what?"
88
89
else:
89
90
print "1 is not 2."
90
91
91
-
Perl 6:
92
+
Perl 6
93
+
92
94
if 1 == 2 {
93
95
say "Wait, what?"
94
96
} else {
95
97
say "1 is not 2."
96
98
}
97
99
98
-
Parentheses are optional in both languages in expressions in conditionals,
99
-
as shown above.
100
+
Parentheses are optional in both languages in expressions in
101
+
conditionals, as shown above.
100
102
101
103
=head2Variables
102
104
@@ -119,8 +121,9 @@ hold arrays, and variables starting with a C<%> hold a hash (dict).
119
121
Immutable variables can be sigil-less, if they are declared with a C<\>.
120
122
121
123
Python
124
+
122
125
s = 10
123
-
l = [1,2,3]
126
+
l = [1, 2, 3]
124
127
d = { a : 12, b : 99 }
125
128
126
129
print s
@@ -129,6 +132,7 @@ Python
129
132
# 10, 2, 12
130
133
131
134
Perl 6
135
+
132
136
my $s = 10;
133
137
my @l = 1, 2, 3;
134
138
my %d = a => 12, b => 99;
@@ -144,18 +148,20 @@ Perl 6
144
148
=head2Scope
145
149
146
150
In Python, functions and classes create a new scope, but no other
147
-
block constructor (e.g. loops, conditionals) creates a scope. In Python 2,
148
-
list comprehensions do not create a new scope, but in Python 3, they do.
151
+
block constructor (e.g. loops, conditionals) creates a scope. In
152
+
Python 2, list comprehensions do not create a new scope, but in Python 3, they do.
149
153
150
154
In Perl 6, every block creates a lexical scope.
151
155
152
156
Python
157
+
153
158
if True:
154
159
x = 10
155
160
print x
156
161
# x is now 10
157
162
158
163
Perl 6
164
+
159
165
if True {
160
166
my $x = 10
161
167
}
@@ -170,13 +176,15 @@ Perl 6
170
176
# ok, x is 10
171
177
172
178
Python
179
+
173
180
x = 10
174
181
for x in 1, 2, 3:
175
182
pass
176
183
print x
177
184
# x is 3
178
185
179
186
Perl 6
187
+
180
188
my \x = 10;
181
189
for 1, 2, 3 -> \x {
182
190
# do nothing
@@ -187,13 +195,17 @@ Perl 6
187
195
Lambdas in Python can be written as blocks or pointy blocks in Perl 6.
188
196
189
197
Python
198
+
190
199
l = lambda i: i + 12
200
+
191
201
Perl 6
202
+
192
203
$l = -> $i { $i + 12 }
193
204
194
205
Another Perl 6 idiom for constructing lambdas is the Whatever star, C<*>.
195
206
196
207
Perl 6
208
+
197
209
$l = * + 12 # same as above
198
210
199
211
A C<*> in an expression will become a placeholder for the argument,
@@ -205,6 +217,7 @@ See the section below for more constructs regarding subroutines and blocks.
205
217
Another example (from the Python L<FAQ|https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result>):
206
218
207
219
Python
220
+
208
221
squares = []
209
222
for x in range(5):
210
223
squares.append(lambda: x ** 2)
@@ -213,16 +226,17 @@ Python
213
226
# both 16 since there is only one x
214
227
215
228
Perl 6
216
-
my \squares = [];
229
+
230
+
my @squares;
217
231
for ^5 -> \x {
218
-
squares.append({ x² });
232
+
@squares.append({ x² });
219
233
}
220
-
say squares[2]();
221
-
say squares[4]();
234
+
say @squares[2]();
235
+
say @squares[4]();
222
236
# 4, 16 since each loop iteration has a lexically scoped x,
223
237
224
238
Note that C<^N> is like C<range(N)>. Similarly,
225
-
C<N..^M> works like C<range(N,M)> (a list from N
239
+
C<N..^M> works like C<range(N,M)> (a list from N
226
240
to M - 1). C<N..M> is a list from N to M. The
227
241
C<^> before or after the C<..> indicates that the
228
242
beginning or ending endpoint of the list (or both)
@@ -238,6 +252,7 @@ or symbol that can be used in Perl 6 has an ASCII equivalent
238
252
=head2Control Flow
239
253
240
254
Python has C<for> loops and C<while> loops:
255
+
241
256
for i in 1, 2:
242
257
print i
243
258
j = 1
@@ -248,6 +263,7 @@ Python has C<for> loops and C<while> loops:
248
263
# 1, 2, 1, 2
249
264
250
265
Perl 6 also has C<for> loops and C<while> loops:
266
+
251
267
for 1, 2 -> $i {
252
268
say $i
253
269
}
@@ -265,6 +281,7 @@ C<break> in Python. C<continue> in Python is C<next>
265
281
in Perl 6.
266
282
267
283
Python
284
+
268
285
for i in range(10):
269
286
if i == 3:
270
287
continue
@@ -273,6 +290,7 @@ Python
273
290
print i
274
291
275
292
Perl 6
293
+
276
294
for ^10 -> $i {
277
295
next if $i == 3;
278
296
last if $i == 5;
@@ -341,6 +359,7 @@ In Perl 6, positional and named arguments are determined
341
359
by the signature of the routine.
342
360
343
361
Python
362
+
344
363
def speak(word, times):
345
364
for i in range(times):
346
365
print word
@@ -399,7 +418,7 @@ Perl 6
399
418
$square = { $^x ** 2 }; # placeholder variable
400
419
$square = { $_ ** 2 }; # topic variable
401
420
402
-
Placeholder variables are lexicographically ordered to form positional parameters.
421
+
Placeholder variables are put in lexicographic order to form positional parameters.
403
422
i.e. these are the same:
404
423
405
424
$power = { $^x ** $^y }
@@ -418,25 +437,26 @@ Perl 6
418
437
( -> \i { i * 2 } for 3, 9 );
419
438
( { $^i * 2 } for 3, 9 );
420
439
( { $_ * 2 } for 3, 9 );
440
+
( * * 2 for 3, 9 );
421
441
422
442
Conditionals can be applied, but the C<if> comes first,
423
443
unlike in Python where the if comes second.
424
444
425
-
[ x * 2 for x in 1, 2, 3 if x > 1 ]
445
+
[ x * 2 for x in 1, 2, 3 if x > 1 ] # Python
426
446
427
447
vs
428
448
429
-
( $_ * 2 if $_ > 1 for 1, 2, 3 );
449
+
( $_ * 2 if $_ > 1 for 1, 2, 3 ) # Perl 6
430
450
431
451
For nested loops, the cross product operator C<X>
432
452
will help:
433
453
434
-
[ i + j for i in 3,9 for j in 2,10 ]
454
+
[ i + j for i in 3,9 for j in 2,10 ] # Python
435
455
436
456
becomes either of these:
437
457
438
-
( { $_[0] + $_[1] } for (3,9) X (2,10) );
439
-
( -> (\i, \j) { i + j } for (3,9) X (2,10) );
458
+
( { $_[0] + $_[1] } for (3,9) X (2,10) ); # Perl 6, topic variable
459
+
( -> (\i, \j) { i + j } for (3,9) X (2,10) ); # Perl 6, pointy block
440
460
441
461
Using C<map> (which is just like Python's C<map>) is
442
462
an alternative.
@@ -447,11 +467,13 @@ Here's an example from the Python L<docs|https://docs.python.org/3/tutorial/clas
447
467
First, "instance variables", aka attributes in Perl 6:
448
468
449
469
Python:
470
+
450
471
class Dog:
451
472
def __init__(self, name):
452
473
self.name = name
453
474
454
475
Perl 6:
476
+
455
477
class Dog {
456
478
has $.name;
457
479
}
@@ -460,12 +482,14 @@ Constructors by default take named arguments in Perl 6,
460
482
and use the method C<new>.
461
483
462
484
Python
485
+
463
486
d = Dog('Fido')
464
487
e = Dog('Buddy')
465
488
print d.name
466
489
print e.name
467
490
468
491
Perl 6
492
+
469
493
my $d = Dog.new(:name<Fido>);
470
494
my $e = Dog.new(:name<Buddy>);
471
495
say $d.name;
@@ -475,6 +499,7 @@ Class attributes in Perl 6 can be declared in a few ways. One way
475
499
is to just declare a lexical variable and a method for accessing it.
476
500
477
501
Python:
502
+
478
503
class Dog:
479
504
kind = 'canine' # class attribute
480
505
def __init__(self, name):
@@ -487,6 +512,7 @@ Python:
487
512
print e.name
488
513
489
514
Perl 6:
515
+
490
516
class Dog {
491
517
my $kind = 'canine'; # class attribute
492
518
method kind { $kind }
@@ -503,13 +529,15 @@ Perl 6:
503
529
To mutate attributes, in Perl 6 you"ll want to use C<is rw>:
504
530
505
531
Python:
532
+
506
533
class Dog:
507
534
def __init__(self, name):
508
535
self.name = name
509
536
d = Dog()
510
537
d.name = 'rover'
511
538
512
539
Perl 6:
540
+
513
541
class Dog {
514
542
has $.name is rw;
515
543
}
@@ -519,6 +547,7 @@ Perl 6:
519
547
Inheritance is done using C<is>:
520
548
521
549
Python
550
+
522
551
class Animal:
523
552
def jump:
524
553
print "I am jumping"
@@ -530,6 +559,7 @@ Python
530
559
d.jump()
531
560
532
561
Perl 6
562
+
533
563
class Animal {
534
564
method jump {
535
565
say "I am jumping"
@@ -542,9 +572,10 @@ Perl 6
542
572
my $d = Dog.new;
543
573
$d.jump;
544
574
545
-
Multiple inheritance is possible with multipleC<is>traits, or with C<also>.
575
+
Multiple inheritance is possible by usingC<is>multiple times, or with C<also>.
546
576
547
577
Python
578
+
548
579
class Dog(Animal, Friend, Pet):
549
580
pass
550
581
@@ -563,9 +594,10 @@ or
563
594
=head2Decorators
564
595
565
596
Decorators in Perl 6 are a way of wrapping a function
566
-
in another one. In Perl 6 this is done with C<wrap>.
597
+
in another one. In Perl 6, this is done with C<wrap>.
567
598
568
599
Python
600
+
569
601
def greeter(f):
570
602
def new():
571
603
print 'hello'
@@ -579,6 +611,7 @@ Python
579
611
world();
580
612
581
613
Perl 6
614
+
582
615
sub world {
583
616
say 'world'
584
617
}
@@ -590,7 +623,7 @@ Perl 6
590
623
591
624
world;
592
625
593
-
An alternative would be to use a trait:
626
+
To encapsulate the wrapping, it can be done in a trait:
594
627
595
628
# declare the trait 'greeter'
596
629
multi sub trait_mod:<is>(Routine $r, :$greeter) {
@@ -615,7 +648,7 @@ Here's a python context manager that prints the strings
0 commit comments