[issue43628] Incorrect argument errors for random.getstate()
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
New submission from Yang Feng <charles.fy@foxmail.com>: 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 ---------- assignee: docs@python components: Documentation messages: 389535 nosy: CharlesFengY, docs@python priority: normal severity: normal status: open title: Incorrect argument errors for random.getstate() versions: Python 3.10 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43628> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Change by Karthikeyan Singaravelan <tir.karthi@gmail.com>: ---------- nosy: +mark.dickinson, rhettinger _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43628> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: 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? ---------- resolution: -> not a bug stage: -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43628> _______________________________________
participants (3)
-
Karthikeyan Singaravelan
-
Raymond Hettinger
-
Yang Feng