This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Incorrect argument errors for random.getstate()
Type: Stage: resolved
Components: Documentation Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: CharlesFengY, docs@python, mark.dickinson, rhettinger
Priority: normal Keywords:

Created on 2021-03-26 01:58 by CharlesFengY, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg389535 - (view) Author: Yang Feng (CharlesFengY) Date: 2021-03-26 01:58
In documentation of random.getstate(), it says:
“random.getstate()
Return an object capturing the current internal state of the generator. This object can be passed to setstate() to restore the state.”

random.getstate() takes 0 argument and return the current setting for the weekday to start each week. However, when I give one argument to random.getstate(), the interpreter reports the following error:
----------------------------------------------
>>> import random
>>> random.getstate(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: getstate() takes 1 positional argument but 2 were given
----------------------------------------------

Here I have two doubts about the reported errors:
1. Is the TypeError correct? This should be an inconsistent argument number error. There is nothing to do with Type. 
2. Is the detailed error correct? Doc says random.getstate() takes 0 argument, the reported error says getstate() take 1 positional argument. which is inconsistent. Besides, I pass one argument to random.getstate(), but the reported error says 2 were given.


Environment: Python 3.10, Ubuntu 16.04
msg389536 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-03-26 03:00
Here's how to use getstate() and setstate():

>>> import random
>>> state = random.getstate()
>>> random.choices('ABCDE', k=8)
['E', 'A', 'B', 'B', 'A', 'A', 'E', 'D']
>>> # restore the previous state
>>> random.setstate(state)
>>> # now, replay the random selections
>>> random.choices('ABCDE', k=8)
['E', 'A', 'B', 'B', 'A', 'A', 'E', 'D']



> 1. Is the TypeError correct? This should be an inconsistent
> argument number error. There is nothing to do with Type. 

Yes, it is correct, but I agree that it is unintuitive.  It just happens to be the Python way to report an incorrect number of arguments as a TypeError.


> 2. Is the detailed error correct? Doc says random.getstate()
> takes 0 argument, the reported error says getstate() take 1
> positional argument. which is inconsistent. 

This is an artifact of how Python implements object orient programming.  No one really likes this, but at some level it can be viewed as being technically correct — methods prepend an instance argument before calling an underlying function which reports on the number of arguments that it sees.  Presumably, if there were a straight-forward way of improving the error message, it would have been done long ago.

> Besides, I pass one argument to random.getstate(), 
> but the reported error says 2 were given.

Don't pass any arguments into getstate().


> random.getstate() takes 0 argument and return 
> the current setting for the weekday to start each week.

This part doesn't make sense to me.  Why do you think it accepts or returns a weekday?
History
Date User Action Args
2022-04-11 14:59:43adminsetgithub: 87794
2021-03-26 03:00:23rhettingersetstatus: open -> closed
resolution: not a bug
messages: + msg389536

stage: resolved
2021-03-26 02:44:49xtreaksetnosy: + rhettinger, mark.dickinson
2021-03-26 01:58:53CharlesFengYcreate