<Python for Kids>读书笔记

9 Python‘s build-in Functions


dir function

dir using the variable popcorn containing a string value, and you get the list of functions provided by the string class (all strings are members of the string class):
>>> popcorn = 'I love popcorn!'
>>> dir(popcorn)


['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
'__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__',
'__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__',
'__rmul__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', 'capitalize', 'center', 'count', 'encode',
'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index',
'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier',
'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle',
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition',
'replace', 'rfind', 'rindex', 'rjust', 'rpartition',
'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip',
'swapcase', 'title', 'translate', 'upper', 'zfill']


At this point, you could use help to get a short description of any function in the list. Here’s an example of running help against the upper function:
>>> help(popcorn.upper)
Help on built-in function upper:
upper(...)
S.upper() -> str
Return a copy of S converted to uppercase.
The information returned can be a little confusing, so let’s take a closer look. The ellipsis (...) means that upper is a built-in function of the string class and, in this case, takes no parameters. The arrow (->) on the next line means that this function returns a string (str). The last line offers a brief description of what the function
does.

10 Useful Python Modules

shallow copy and deep copy


>>> harry = Animal('hippogriff', 6, 'pink')
>>> carrie = Animal('chimera', 4, 'green polka dots')
>>> billy = Animal('bogill', 0, 'paisley')
>>> my_animals = [harry, carrie, billy]
>>> more_animals = copy.copy(my_animals)
>>> print(more_animals[0].species)
hippogriff
>>> print(more_animals[1].species)
chimera

what happens if we change the species of one of our Animal objects in the original my_animals list (hippogriff to ghoul). Python changes the species in more_animals, too.
>>> my_animals[0].species = 'ghoul'
>>> print(my_animals[0].species)
ghoul
>>> print(more_animals[0].species)
ghoul


That’s odd. Didn’t we change the species in my_animals only? Why did the species change in both lists?
The species changed because copy actually makes a shallow copy, which means it doesn’t copy objects inside the objects we copied.
Here, it has copied the main list object but not the individual objects inside the list. So we end up with a new list that does not have its own new objects—the list more_animals has the same three objects inside it.

By the same token, if we add a new animal to the first list (my_animals), it doesn’t appear in the copy (more_animals). As proof, print the length of each list after adding another animal, like this:
>>> sally = Animal('sphinx', 4, 'sand')
>>> my_animals.append(sally)
>>> print(len(my_animals))
4
>>> print(len(more_animals))
3


As you can see, when we append a new animal to the first list, my_animals, it isn’t added to the copy of that list, more_animals. When we use len and print the results, the first list has four elements and the second has three. 


Another function in the copy module, deepcopy, actually creates copies of all objects inside the object being copied.


12 Tkinter


from tkinter import *
tk = Tk()
canvas = Canvas(tk, width=400, height=400)
canvas.pack()
canvas.create_polygon(10, 10, 10, 60, 50, 35)
def movetriangle(event):
 if event.keysym == 'Up':
 canvas.move(1, 0, -3)
 elif event.keysym == 'Down':
 canvas.move(1, 0, 3)
 elif event.keysym == 'Left':
 canvas.move(1, -3, 0)
 else:

 canvas.move(1, 3, 0)

canvas.bind_all('<KeyPress-Up>', movetriangle)
canvas.bind_all('<KeyPress-Down>', movetriangle)
canvas.bind_all('<KeyPress-Left>', movetriangle)
canvas.bind_all('<KeyPress-Right>', movetriangle)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值