May-31-2020, 03:13 PM
If you type "help(random.choice) you get this:
In Python, a "sequence" is a container data type. Strings are sequences because they are made up of individual letters that you can access. Lists and tuples because they too are made up of things that can be independently accessed.
Your call "random.choice('string1','string2')" would have worked if you left out the second string. It would return a randomly chosen letter from 'string1'. Probably not what you want. To randomly choose between 'string1' and 'string2' you need to put them in a sequence. Because they are smaller and faster I would use a tuple.
If you type "type(random.choice)" it returns <class 'method'>. choice is a method of class Random. It doesn't look like a method. Normally a method is called using an instance; instance.method(), but when you do this:
This confusing syntactic magic is performed using a singleton. When you import random it creates a single instance of class Random. "_inst = Random()". Then it creates some global variables to access the Random methods "choices = _inst.choices". This makes "Random" look like a collection of functions instead of a class, but you are really creating an instance and calling instance methods. When you call "random.choice('string1', 'string2')" your are actually calling "random._inst.choice(string1', 'string2')" which is really "random.Random.choice(_inst, string1', 'string2')". Look at that! Three positional arguments.
Help on method choice in module random: choice(seq) method of random.Random instance Choose a random element from a non-empty sequence.The two interesting pieces of information are that the function is expecting a "sequence" argument, and that the function is not a function.
In Python, a "sequence" is a container data type. Strings are sequences because they are made up of individual letters that you can access. Lists and tuples because they too are made up of things that can be independently accessed.
Your call "random.choice('string1','string2')" would have worked if you left out the second string. It would return a randomly chosen letter from 'string1'. Probably not what you want. To randomly choose between 'string1' and 'string2' you need to put them in a sequence. Because they are smaller and faster I would use a tuple.
random.choice(('string1','string2'))But why did your error message say "choice() takes two positional arguments, but three were given." Where does it see three positional arguments? This was the other interesting thing learned from help. random.choice is not a function.
If you type "type(random.choice)" it returns <class 'method'>. choice is a method of class Random. It doesn't look like a method. Normally a method is called using an instance; instance.method(), but when you do this:
import random x = random.choice(('string1', 'string2'))the "random" is a module, not an instance of class Random. random tells Python to look in the "random" namespace to find "choice" So how can random.choice() call a method and where is the instance?
This confusing syntactic magic is performed using a singleton. When you import random it creates a single instance of class Random. "_inst = Random()". Then it creates some global variables to access the Random methods "choices = _inst.choices". This makes "Random" look like a collection of functions instead of a class, but you are really creating an instance and calling instance methods. When you call "random.choice('string1', 'string2')" your are actually calling "random._inst.choice(string1', 'string2')" which is really "random.Random.choice(_inst, string1', 'string2')". Look at that! Three positional arguments.