新手提问帖:两个小问题。
1,
>>> class Thing(object):
... def test(hi):
... print "hi"
...
>>> a = Thing()
>>> a.test("hello")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: test() takes exactly 1 argument (2 given)
>>>
就是这个吗?嗯,这个是我在Python 命令行下展示给你的一点魔法。你还没有见过class 不过后面
很快就要碰到了。现在你看到Python 说test() takes exactly 1 argument (2 given) (test() 只可以接
受1 个参数,实际上给了两个)。它意味着python 把a.test("hello") 改成了test(a, "hello") ,而有
人弄错了,没有为它添加a 这个参数。
这个我其实还不太懂。
系统为什么会认为test有两个参数呢。
这种情况下,这个test怎么能够正常使用呢。
~
2,
a=raw_input(),如果a是需要一个数字,怎么实现呢?
3,
next=raw_input(">")
if"0" in next or "1" in next:
how_much=int(next)
if"0" in next or "1" in next:
这个判定?
[解决办法]
代码
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> class Thing(object): def test(self,hi): print hi >>> a = Thing()>>> a.test('hello')hello>>> b = raw_input('Please input a number: ')Please input a number: 10>>> print int(b)10>>> print float(b)10.0>>> 'x' in 'xaysdfasdf'True>>> '1' in 'xdfadfadf'False>>>
[解决办法]
你这不是2个问题,是一堆问题。。
[解决办法]
1楼的正解,class 通过self来查找类内部的function,相当于C++的this