function reference != function call
foo.isnumeric
-> 関数参照
foo.isnumeric()
-> 関数呼出し
>>> '23'.isnumeric
<built-in method isnumeric of str object at 0x7feb87baa880>
>>> '23'.isnumeric()
True
Solution.myAtoi()
はただa_to_i()
になります*:
>>> def a_to_i(s):
... if s.isnumeric():
... return int(s)
... else:
... return 0
...
>>> a_to_i('234453')
234453
>>> a_to_i('v%87eb8b')
0
>>> a_to_i('')
0
>>> a_to_i('27.3')
0
>>> # あれっ!?
float型も受け入れる方は:
>>> def a_to_n(s):
... if s.isnumeric():
... return int(s)
... else:
... try:
... return float(s)
... except ValueError:
... return 0
...
>>> a_to_n('3243.245')
3243.245
>>> a_to_n('3243.2452.24523')
0
>>> a_to_n('Frank')
0
>>> a_to_n('25')
25
[string].isnumeric()の出力:
>>> ''.isnumeric()
False
>>> '254231'.isnumeric()
True
>>> '24.143'.isnumeric()
False
>>> # \(O.o)/ 危ないそれ!
...
>>> for x in range(32, 126):
... z = chr(x)
... print(z, z.isnumeric())
...
False
! False
" False
# False
$ False
% False
& False
' False
( False
) False
* False
+ False
, False
- False
. False
/ False
0 True
1 True
2 True
3 True
4 True
5 True
6 True
7 True
8 True
9 True
: False
; False
< False
= False
> False
? False
@ False
A False
B False
C False
D False
E False
F False
G False
H False
I False
J False
K False
L False
M False
N False
O False
P False
Q False
R False
S False
T False
U False
V False
W False
X False
Y False
Z False
[ False
\ False
] False
^ False
_ False
` False
a False
b False
c False
d False
e False
f False
g False
h False
i False
j False
k False
l False
m False
n False
o False
p False
q False
r False
s False
t False
u False
v False
w False
x False
y False
z False
{ False
| False
} False
(* Python != Java. あるclassとそのclassのinstanceとは違いがなければclassじゃなくてただのfunctionを作った方が良いです。selfを使わないmethodはJavaの"static method"か"class method"と同じですが、Pythonではclass methodの方はあんまり使いません。なぜならpython moduleは陰のclassなので、class methodはinner class methodになります。)
(str.isnumeric) or str!=""
はstr.isnumeric() and str!=""
ではないでしょうか。str.isnumeric()
にしていますか?(メソッドの呼び出しなので()
が必要です)