Taken from the existing example.py:
DNA_TO_RNA = str.maketrans("AGCT", "UCGA")
def to_rna(dna_strand):
return dna_strand.translate(DNA_TO_RNA)
- [Static Methods][static-methods]: Distinct from built-in functions, instance methods, and class methods, these are methods that are bound to a class, rather than an instance, and called without explicitly or implicitly passing in an object of the class. The example solution for this exercise uses the
static
str
methodmaketrans
. - [String Methods][string-methods]: this exercise uses
str.maketrans()
(a static method ofstr
that returns a dictionary to create a translation table as required by thestr.translate()
instance method. This method is unusual in that it takes either a single dictionary or two strings of equal length. The example solution for this exercise usesstr.maketrans()
with a two-string argument. - [Dictionary][dictionary]: mapping type that has key-value pairs. Returned by
str.maketrans
in the example code. Also one of the argument types accepted bystr.maketrans()
. - [String Translation][string-translation]: the
str.translate()
instance method is called on an object from thestr
class (e.g.<my string>
.translate()). Returns a copy of the initial string with each character re-mapped through the given translation table. The translation table is typically a mapping or sequence type that implements indexing via the magic method__getitem__()
. - [Function][function]: A named (and often reusable) section of code that performs a specific task. It may or may not have arguments passed in, and may or may not return data. Created using the
def
keyword. - [Function Arguments][function-arguments]: Parameters passed into a function. In python, these are noted in the
()
following a function name. The example code uses a function namedto_rna()
with an argument ofdna_strand
. - [Return Value][return-value]: the
return
keyword is used in a return statement at the end of a function. Exits a function and may or may not pass data or an expression back to calling code. Functions in python without an explicitreturn
keyword and statement will return (pass back) the singleton objectnone
. The example code returns a copy of the passed-in argument (assumed to be a string) that has been mapped throughstr.translate()
, using the table made fromstr.maketrans()