|
358 | 358 | "</table>\n",
|
359 | 359 | "\n",
|
360 | 360 | "\n",
|
361 |
| - "- c. If the sequence passed in above contains `N` bases, use the mean weight of the other bases as the weight." |
| 361 | + "- c. If the sequence passed contains base `N`, use the mean weight of the other bases as the weight of base `N`." |
362 | 362 | ]
|
363 | 363 | },
|
364 | 364 | {
|
|
455 | 455 | "cell_type": "markdown",
|
456 | 456 | "metadata": {},
|
457 | 457 | "source": [
|
| 458 | + "### Mandatory arguments\n", |
| 459 | + "\n", |
458 | 460 | "The arguments we have passed to functions so far have all been _mandatory_, if we do not supply them or if supply the wrong number of arguments python will throw an error also called an exception:"
|
459 | 461 | ]
|
460 | 462 | },
|
|
467 | 469 | "outputs": [],
|
468 | 470 | "source": [
|
469 | 471 | "def square(number):\n",
|
470 |
| - " y = number*number\n", |
471 |
| - " return y" |
| 472 | + " # one mandatory argument\n", |
| 473 | + " y = number*number\n", |
| 474 | + " return y" |
472 | 475 | ]
|
473 | 476 | },
|
474 | 477 | {
|
|
486 | 489 | "cell_type": "markdown",
|
487 | 490 | "metadata": {},
|
488 | 491 | "source": [
|
489 |
| - "Mandatory arguments are assumed to come in the same order as the arguments in the function definition, but you can also opt to specify the arguments using the argument names as _keywords_, supplying the values corresponding to each keyword with a `=` sign." |
| 492 | + "**Mandatory arguments are assumed to come in the same order as the arguments in the function definition**, but you can also opt to specify the arguments using the argument names as _keywords_, supplying the values corresponding to each keyword with a `=` sign." |
490 | 493 | ]
|
491 | 494 | },
|
492 | 495 | {
|
|
509 | 512 | "outputs": [],
|
510 | 513 | "source": [
|
511 | 514 | "def repeat(seq, n):\n",
|
| 515 | + " # two mandatory arguments\n", |
512 | 516 | " result = ''\n",
|
513 | 517 | " for i in range(0,n):\n",
|
514 | 518 | " result += seq\n",
|
|
533 | 537 | },
|
534 | 538 | "outputs": [],
|
535 | 539 | "source": [
|
536 |
| - "print(repeat(seq=\"CTA\", 3))" |
| 540 | + "print(repeat(seq=\"CTA\", n=3))" |
537 | 541 | ]
|
538 | 542 | },
|
539 | 543 | {
|
540 | 544 | "cell_type": "markdown",
|
541 | 545 | "metadata": {},
|
542 | 546 | "source": [
|
| 547 | + "### Arguments with default values\n", |
543 | 548 | "Sometimes it is useful to give some arguments a default value that the caller can override, but which will be used if the caller does not supply a value for this argument. We can do this by assigning some value to the named argument with the `=` operator in the function definition."
|
544 | 549 | ]
|
545 | 550 | },
|
|
579 | 584 | " \n",
|
580 | 585 | "myFunction()\n",
|
581 | 586 | "myFunction()\n",
|
| 587 | + "myFunction()\n", |
| 588 | + "myFunction([])\n", |
| 589 | + "myFunction([])\n", |
582 | 590 | "myFunction([])"
|
583 | 591 | ]
|
584 | 592 | },
|
585 | 593 | {
|
586 | 594 | "cell_type": "markdown",
|
587 | 595 | "metadata": {},
|
588 | 596 | "source": [
|
589 |
| - "One can either create a \"new\" default variable every time a function is run:" |
| 597 | + "... or avoid modifying *mutable* default arguments." |
590 | 598 | ]
|
591 | 599 | },
|
592 | 600 | {
|
|
597 | 605 | },
|
598 | 606 | "outputs": [],
|
599 | 607 | "source": [
|
600 |
| - "def myFunction(parameters=None):\n", |
601 |
| - " \n", |
602 |
| - " if parameters is None:\n", |
603 |
| - " parameters = []\n", |
604 |
| - " \n", |
| 608 | + "def myFunction(parameters):\n", |
| 609 | + " # one mandatory argument without default value\n", |
605 | 610 | " parameters.append( 100 )\n",
|
606 | 611 | " print(parameters)\n",
|
607 | 612 | " \n",
|
608 |
| - "myFunction()\n", |
609 |
| - "myFunction()\n", |
610 |
| - "myFunction([80])\n", |
611 |
| - "myFunction([80])" |
| 613 | + "my_list = []\n", |
| 614 | + "myFunction(my_list)\n", |
| 615 | + "myFunction(my_list)\n", |
| 616 | + "myFunction(my_list)\n", |
| 617 | + "my_new_list = []\n", |
| 618 | + "myFunction(my_new_list)" |
612 | 619 | ]
|
613 | 620 | },
|
614 | 621 | {
|
615 | 622 | "cell_type": "markdown",
|
616 | 623 | "metadata": {},
|
617 | 624 | "source": [
|
618 |
| - "... or avoid modifying *mutable* default arguments.\n", |
619 |
| - "\n", |
620 |
| - "By using the `+` operator which returns the result of the expression as a new value, instead of `list.extend()` that modified the list in-place and returns nothing." |
| 625 | + "### Position of mandatory arguments\n", |
| 626 | + "Arrange function arguments so that *mandatory* arguments come first:" |
621 | 627 | ]
|
622 | 628 | },
|
623 | 629 | {
|
|
628 | 634 | },
|
629 | 635 | "outputs": [],
|
630 | 636 | "source": [
|
631 |
| - "def myFunction(parameters=[]):\n", |
632 |
| - " results = parameters + [100]\n", |
633 |
| - " print(results)\n", |
| 637 | + "def runSimulation(initialTemperature, nsteps=1000):\n", |
| 638 | + " # one mandatory argument followed by one with default value\n", |
| 639 | + " print(\"Running simulation starting at\", initialTemperature, \"K and doing\", nsteps, \"steps\")\n", |
634 | 640 | " \n",
|
635 |
| - "myFunction()\n", |
636 |
| - "myFunction()" |
| 641 | + "runSimulation(300, 500)\n", |
| 642 | + "runSimulation(300)" |
637 | 643 | ]
|
638 | 644 | },
|
639 | 645 | {
|
640 | 646 | "cell_type": "markdown",
|
641 | 647 | "metadata": {},
|
642 | 648 | "source": [
|
643 |
| - "Arrange function arguments so that *mandatory* arguments come first:" |
| 649 | + "As before, no positional argument can appear after a keyword argument, and all required arguments must still be provided." |
644 | 650 | ]
|
645 | 651 | },
|
646 | 652 | {
|
|
651 | 657 | },
|
652 | 658 | "outputs": [],
|
653 | 659 | "source": [
|
654 |
| - "def runSimulation(initialTemperature, nsteps=1000):\n", |
655 |
| - " print(\"Running simulation starting at\", initialTemperature, \"K and doing\", nsteps, \"steps\")\n", |
656 |
| - " \n", |
657 |
| - "runSimulation(300, 500)\n", |
658 |
| - "runSimulation(300)" |
| 660 | + "runSimulation( nsteps=100, initialTemperature=300 )" |
659 | 661 | ]
|
660 | 662 | },
|
661 | 663 | {
|
|
666 | 668 | },
|
667 | 669 | "outputs": [],
|
668 | 670 | "source": [
|
669 |
| - "def badFunction(nsteps=1000, initialTemperature):\n", |
670 |
| - " pass\n" |
671 |
| - ] |
672 |
| - }, |
673 |
| - { |
674 |
| - "cell_type": "markdown", |
675 |
| - "metadata": {}, |
676 |
| - "source": [ |
677 |
| - "As before, no positional argument can appear after a keyword argument, and all required arguments must still be provided." |
| 671 | + "runSimulation( initialTemperature=300 )" |
678 | 672 | ]
|
679 | 673 | },
|
680 | 674 | {
|
|
685 | 679 | },
|
686 | 680 | "outputs": [],
|
687 | 681 | "source": [
|
688 |
| - "runSimulation( nsteps=100, 300 )" |
| 682 | + "runSimulation( nsteps=100 ) # Error: missing required argument 'initialTemperature'" |
689 | 683 | ]
|
690 | 684 | },
|
691 | 685 | {
|
|
696 | 690 | },
|
697 | 691 | "outputs": [],
|
698 | 692 | "source": [
|
699 |
| - "runSimulation( nsteps=100 )" |
| 693 | + "runSimulation( nsteps=100, 300 ) # Error: positional argument follows keyword argument" |
700 | 694 | ]
|
701 | 695 | },
|
702 | 696 | {
|
|
714 | 708 | },
|
715 | 709 | "outputs": [],
|
716 | 710 | "source": [
|
717 |
| - "runSimulation( initialTemperature=300, numSteps=100 )" |
| 711 | + "runSimulation( initialTemperature=300, numSteps=100 ) # Error: unexpected keyword argument 'numSteps'" |
| 712 | + ] |
| 713 | + }, |
| 714 | + { |
| 715 | + "cell_type": "markdown", |
| 716 | + "metadata": {}, |
| 717 | + "source": [ |
| 718 | + "Function cannot be defined with mandatory arguments after default ones." |
718 | 719 | ]
|
719 | 720 | },
|
720 | 721 | {
|
|
725 | 726 | },
|
726 | 727 | "outputs": [],
|
727 | 728 | "source": [
|
728 |
| - "def runSimulation(initialTemperature, nsteps=1000):\n", |
729 |
| - " print(\"Running simulation starting at\", initialTemperature, \"K and doing\", nsteps, \"steps\")\n", |
730 |
| - " \n", |
731 |
| - "runSimulation(initialTemperature=300, nsteps=500)\n", |
732 |
| - "runSimulation(initialTemperature=300)" |
| 729 | + "def badFunction(nsteps=1000, initialTemperature):\n", |
| 730 | + " pass" |
733 | 731 | ]
|
734 | 732 | },
|
735 | 733 | {
|
|
0 commit comments