Function takes string python

How do I use strings to call functions/methods?

    The best is to use a dictionary that maps strings to functions. The primary advantage of this technique is that the strings do not need to match the names of the functions. This is also the primary technique used to emulate a case construct:

def a(): pass def b(): pass dispatch = 'go': a, 'stop': b> # Note lack of parens for funcs dispatch[get_input()]() # Note trailing parens to call function 
import foo getattr(foo, 'bar')() 

Note that getattr() works on any object, including classes, class instances, modules, and so on. This is used in several places in the standard library, like this:

class Foo: def do_foo(self): . def do_bar(self): . f = getattr(foo_instance, 'do_' + opname) f()
def myFunc(): print("hello") fname = "myFunc" f = locals()[fname] f()

Is there an equivalent to Perl’s chomp() for removing trailing newlines from strings?

You can use S.rstrip(«\r\n») to remove all occurrences of any line terminator from the end of the string S without removing other trailing whitespace. If the string S represents more than one line, with several empty lines at the end, the line terminators for all the blank lines will be removed:

>>> lines = ("line 1 \r\n" . "\r\n" . "\r\n") >>> lines.rstrip("\n\r") 'line 1 '

Since this is typically only desired when reading text one line at a time, using S.rstrip() this way works well.

Читайте также:  Javascript двумерный массив push

Is there a scanf() or sscanf() equivalent?

For simple input parsing, the easiest approach is usually to split the line into whitespace-delimited words using the split() method of string objects and then convert decimal strings to numeric values using int() or float() . split() supports an optional “sep” parameter which is useful if the line uses something other than whitespace as a separator.

For more complicated input parsing, regular expressions are more powerful than C’s sscanf() and better suited for the task.

What does ‘UnicodeDecodeError’ or ‘UnicodeEncodeError’ error mean?

Performance

My program is too slow. How do I speed it up?

That’s a tough one, in general. First, here are a list of things to remember before diving further:

  • Performance characteristics vary across Python implementations. This FAQ focuses on CPython.
  • Behaviour can vary across operating systems, especially when talking about I/O or multi-threading.
  • You should always find the hot spots in your program before attempting to optimize any code (see the profile module).
  • Writing benchmark scripts will allow you to iterate quickly when searching for improvements (see the timeit module).
  • It is highly recommended to have good code coverage (through unit testing or any other technique) before potentially introducing regressions hidden in sophisticated optimizations.

That being said, there are many tricks to speed up Python code. Here are some general principles which go a long way towards reaching acceptable performance levels:

  • Making your algorithms faster (or changing to faster ones) can yield much larger benefits than trying to sprinkle micro-optimization tricks all over your code.
  • Use the right data structures. Study documentation for the Built-in Types and the collections module.
  • When the standard library provides a primitive for doing something, it is likely (although not guaranteed) to be faster than any alternative you may come up with. This is doubly true for primitives written in C, such as builtins and some extension types. For example, be sure to use either the list.sort() built-in method or the related sorted() function to do sorting (and see the Sorting HOW TO for examples of moderately advanced usage).
  • Abstractions tend to create indirections and force the interpreter to work more. If the levels of indirection outweigh the amount of useful work done, your program will be slower. You should avoid excessive abstraction, especially under the form of tiny functions or methods (which are also often detrimental to readability).
Читайте также:  Json schema to pojo java

If you have reached the limit of what pure Python can allow, there are tools to take you further away. For example, Cython can compile a slightly modified version of Python code into a C extension, and can be used on many different platforms. Cython can take advantage of compilation (and optional type annotations) to make your code significantly faster than when interpreted. If you are confident in your C programming skills, you can also write a C extension module yourself.

What is the most efficient way to concatenate many strings together?

str and bytes objects are immutable, therefore concatenating many strings together is inefficient as each concatenation creates a new object. In the general case, the total runtime cost is quadratic in the total string length.

To accumulate many str objects, the recommended idiom is to place them into a list and call str.join() at the end:

chunks = [] for s in my_strings: chunks.append(s) result = ''.join(chunks)

(another reasonably efficient idiom is to use io.StringIO )

To accumulate many bytes objects, the recommended idiom is to extend a bytearray object using in-place concatenation (the += operator):

result = bytearray() for b in my_bytes_objects: result += b

Sequences (Tuples/Lists)

How do I convert between tuples and lists?

The type constructor tuple(seq) converts any sequence (actually, any iterable) into a tuple with the same items in the same order.

For example, tuple([1, 2, 3]) yields (1, 2, 3) and tuple(‘abc’) yields (‘a’, ‘b’, ‘c’) . If the argument is a tuple, it does not make a copy but returns the same object, so it is cheap to call tuple() when you aren’t sure that an object is already a tuple.

The type constructor list(seq) converts any sequence or iterable into a list with the same items in the same order. For example, list((1, 2, 3)) yields [1, 2, 3] and list(‘abc’) yields [‘a’, ‘b’, ‘c’] . If the argument is a list, it makes a copy just like seq[:] would.

What’s a negative index?

Python sequences are indexed with positive numbers and negative numbers. For positive numbers 0 is the first index 1 is the second index and so forth. For negative indices -1 is the last index and -2 is the penultimate (next to last) index and so forth. Think of seq[-n] as the same as seq[len(seq)-n] .

Using negative indices can be very convenient. For example S[:-1] is all of the string except for its last character, which is useful for removing the trailing newline from a string.

How do I iterate over a sequence in reverse order?

Use the reversed() built-in function:

for x in reversed(sequence): . # do something with x . 

This won’t touch your original sequence, but build a new copy with reversed order to iterate over.

How do you remove duplicates from a list?

See the Python Cookbook for a long discussion of many ways to do this:

If you don’t mind reordering the list, sort it and then scan from the end of the list, deleting duplicates as you go:

if mylist: mylist.sort() last = mylist[-1] for i in range(len(mylist)-2, -1, -1): if last == mylist[i]: del mylist[i] else: last = mylist[i]

If all elements of the list may be used as set keys (i.e. they are all hashable) this is often faster

This converts the list into a set, thereby removing duplicates, and then back into a list.

How do you remove multiple items from a list

As with removing duplicates, explicitly iterating in reverse with a delete condition is one possibility. However, it is easier and faster to use slice replacement with an implicit or explicit forward iteration. Here are three variations.:

mylist[:] = filter(keep_function, mylist) mylist[:] = (x for x in mylist if keep_condition) mylist[:] = [x for x in mylist if keep_condition] 

The list comprehension may be fastest.

How do you make an array in Python?

Lists are equivalent to C or Pascal arrays in their time complexity; the primary difference is that a Python list can contain objects of many different types.

The array module also provides methods for creating arrays of fixed types with compact representations, but they are slower to index than lists. Also note that NumPy and other third party packages define array-like structures with various characteristics as well.

To get Lisp-style linked lists, you can emulate cons cells using tuples:

lisp_list = ("like", ("this", ("example", None) ) )

If mutability is desired, you could use lists instead of tuples. Here the analogue of lisp car is lisp_list[0] and the analogue of cdr is lisp_list[1] . Only do this if you’re sure you really need to, because it’s usually a lot slower than using Python lists.

How do I create a multidimensional list?

You probably tried to make a multidimensional array like this:

This looks correct if you print it:

>>> A [[None, None], [None, None], [None, None]]

But when you assign a value, it shows up in multiple places:

>>> A[0][0] = 5 >>> A [[5, None], [5, None], [5, None]] 

The reason is that replicating a list with * doesn’t create copies, it only creates references to the existing objects. The *3 creates a list containing 3 references to the same list of length two. Changes to one row will show in all rows, which is almost certainly not what you want.

The suggested approach is to create a list of the desired length first and then fill in each element with a newly created list:

A = [None] * 3 for i in range(3): A[i] = [None] * 2 

This generates a list containing 3 different lists of length two. You can also use a list comprehension:

w, h = 2, 3 A = [[None] * w for i in range(h)]

Or, you can use an extension that provides a matrix datatype; NumPy is the best known.

Python 3.11

The canonical way to share information across modules within single program is create special (often called config cfg).

Use a list comprehension: This because of combination the fact that augmented assignment operators are and difference between mutable immutable objects

This answer actually applies to methods, but the question usually comes up first in context of constructors.

Источник

Calling a Function From String Name in Python

Calling a Function From String Name in Python

  1. Use getattr() to Assign a Function Into a Variable in Python
  2. Use locals() and globals() to Call a Function From a String in Python

This tutorial will introduce how to call a function using its name in string format in Python.

The use-case for this problem is to assign a function from a module or a class into a variable for whatever use it may have.

Use getattr() to Assign a Function Into a Variable in Python

The function getattr() returns a value of an attribute from an object or module. This function has two required arguments, the first argument is the name of the object or module, and the second is a string value that contains the name of the attribute.

The attribute in question may be in the form of a variable, a function, or a subclass.

Let’s say we have a class named User with the given attributes:

# Filename: user.py class User():  name = 'John'  age = 33  def doSomething():  print(name + ' did something.') 

Now, we want to store the attribute function doSomething() into a method and call it. To do this, we’ll use the getattr() function.

from user import User as user  doSomething = getattr(user, 'doSomething')  doSomething(user) 

Now, the function user.doSomething() is wrapped within the variable doSomething . This way, the object user doesn’t have to be specified to call the function.

Use locals() and globals() to Call a Function From a String in Python

Another way to call a function from a string is by using the built-in functions locals() and globals . These two functions return a Python dictionary that represents the current symbol table of the given source code.

The difference between the two functions is the namespace. As the names indicate, locals() returns a dictionary including local variables and globals() returns a dictionary including local variables. Function names are also returned in the format of the string.

Источник

Оцените статью