|
|
发表于 2015-10-7 16:42:22
|
显示全部楼层
return和print毫无关系的,你的例子中a() = 'I love fishC.com',也就是函数a()的值是'I love fishC.com'的字符串,这样我们可以对a()进行字符串的一些列操作例如:
- >>> def a():
- return("I love fishC.com")
- >>> a() == "I love fishC.com"
- True
- >>> a() == print("I love fishC.com")
- I love fishC.com
- False
- >>> a()[0]
- 'I'
- >>> a()[::-1]
- 'moc.Chsif evol I'
- >>>
复制代码
即对a()切片以及反转等等字符串的操作!!
可以看到a()和print('I love fishC.com')是不等的。
特别是后面学到类的时候,你这种思维会导致你更难理解类以及python的魔法方法。
其实你们课后题做到30课了很不错了,我只做到了27课(从课后题的回复痕迹可以看出),上午的时候花了10鱼币看了下楼主说的30课后作业。我是把小甲鱼的教程看到20多讲后停在那儿,抽着看,先把网易的英语python看完后,再把《像计算机科学家一样思考python》这本书给看完了!
关于return,我刚写了关于类的例子给你简单说下,顺便把后面41-45讲左右的魔法方法说下.
- class Aaa:
- def __new__(cls,name,value):
- print('nihao')
- return Bbb(name,value)
- def __init__(self,name,value):
- self.name = name**2
- self.value = value*2
- class Bbb:
- def __new__(cls,name,value):
- print('shnm')
- return Ccc(name,value)
- def __init__(self,fish,num):
- self.fish = fish
- self.num = num
- class Ccc:
- def __init__(self,name,value):
- self.name = [1,2,3,name]
- self.value = value**3
- self.pompt = '雾草'
- self._woco = ''
- def __len__(self):
- print('11')
- return len([1,2,3])
- def __del__(self):
- print('shanle')
- def __str__(self):
- print('beidiaoyong')
- return str(self.name)
- def __repr__(self):
- return self.__str__()
- def __getattr__(self,name):
- print('buzai')
- self.pompt = '被更新'
- def __set__(self,instance,name):
- print('set')
- self._name = name.title()
- def __get__(self,instance,owner):
- print('quqiu')
- return self._name
- def __delete__(self,instance):
- print(instance)
- def __add__(self,other):
- if type(other) == type(1):
- return self.value+other
- if type(other) == type(self):
- return self.value + other.value
- def __sub__(self,other):
- return self.value - other.value
- def __mul__(self,other):
- return other.__sub__(self)
- def __truediv__(self,other):
- return self.value/other.value
- def __radd__(self,other):
- return self.__add__(other)
- def __It__(self,other):
- if self.value > other.value:
- return False
- if self.value < other.value:
- return True
- class Eee:
- def __init__(self):
- self.x = 2
- self.y = 4
- def __call__(self,a,b):
- return Ccc(a,b)
复制代码
我们运行一下,然后在shell里面输入下面的调用:
>>> mmm = Eee()
>>> ff = mmm(2,5)
>>> ff.name
[1, 2, 3, 2]
>>> ff.value
125
>>> ff.pompt
'雾草'
>>> ff.wocao
buzai
>>> ff.pompt
'被更新'
>>> 1 + ff
126
>>> ff + 1
126
>>> len(ff)
11
3
>>> nn = mmm(3,4)
>>> nn.name
[1, 2, 3, 3]
>>> nn.value
64
>>> ff + nn
189
>>> nn + ff
189
>>> ff/nn
1.953125
>>> nn/ff
0.512
>>> del ff
shanle |
|