关于python类和实例的一些尝试

这里写图片描述

这里写图片描述

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
>>> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值