Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> a=5
>>> b=5
>>> de
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
de
NameError: name 'de' is not defined
>>> def x(a,b):
return a,b
>>> x(a,b)
(5, 5)
>>> type(x(a,b))
<class 'tuple'>
>>> def y(a,b):
return [a,b]
>>> y(a,b)
[5, 5]
>>> x(a,b)[0]
5
>>> class student(object):
def sum(self):
a=5
b=6
return a+b
>>> student.sum
<function student.sum at 0x0000025F1939A9D8>
>>> class student(object):
def __init__(self):
self.a=5
self.b=6
def MySum(self):
return self.a+self.b
>>> c=student.MySum
>>> c
<function student.MySum at 0x0000025F1939AAE8>
>>> class student(object):
def MySum(self,a,b):
return a+b
>>> c=student.MySum(5,6)
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
c=student.MySum(5,6)
TypeError: MySum() missing 1 required positional argument: 'b'
>>> class student(object):
def __init__(self):
self.mySum=0
def MySum(self,a,b):
self.mySum=a+b
return self.mySum
>>> c=student.MySum(5,6)
Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
c=student.MySum(5,6)
TypeError: MySum() missing 1 required positional argument: 'b'
>>> c=student()
>>> c.MySum(5,6)
11
>>> class student(object):
def __init__(self,a,b):
self.mySum=a+b
return self.mySum
>>> student(5,6)
Traceback (most recent call last):
File "<pyshell#41>", line 1, in <module>
student(5,6)
TypeError: __init__() should return None, not 'int'
>>> class student(object):
def __init__(self,a,b):
self.mySum=a+b
>>> student(5,6).mySum
11
>>>
关于python类和实例的一些尝试
最新推荐文章于 2022-08-09 21:21:09 发布