Letter to a young programmer
Dear (insert name here),
I heard you enjoy a certain programming language named Python. Programming is a wonderful activity. I am a little jealous that you have access to computers at your age; when I grew up I didn't even know what a computer was! I was an electronics hobbyist though, and my big dream was to build my own electronic calculator from discrete components. I never did do that, but I did build several digital clocks, and it was amazing to build something that complex and see it work. I hope you dream big too -- programmerscanmake computers (and robots!) do amazing things, and this is a great time to become a programmer. Just imagine how much faster computers will be in five or ten years, and what you will be able to do with your skills then!
--Guido van Rossum (inventor of Python) Thursday, October 24, 2013
What's the Best Way for a Programmer to Learn a New Language?
set:
https://docs.python.org/2/library/sets.html
>>>a = set(['br-tun', 'br-ex'])
>>>b = set(['br-int','br-ex'])
>>> a.difference_update(b)
>>>a
set(['br-tun'])
1、
def Dyanmic(**dic):
for i in dic.keys():locals()[i] = dic[i]
print locals()
print dic.keys()
Dyanmic(a=2,d=0,c=1)
------------------------------->
{'i': 'd', 'a': 2, 'c': 1, 'd': 0, 'dic': {'a': 2, 'c': 1, 'd': 0}}
['a', 'c', 'd']
2、from collections import namedtuple
3、 Closure
def func1(a):
def func2(b):
return a+b
return func2
----->
p = func1(11)
q = func1(22)
print p(100), q(100) ----> 111,122
4、 Sort fun
5、 一个简单的例子说明classmethod,classmeth
class A(object):
@classmethod
def func1(cls):
print "A.func1", cls.__name__
@classmethod
def func2(cls):
cls.func1()
print "func2", cls.__name__
class B(A):
@classmethod
def func1(cls):
print "B.func1", cls.__name__
A.func2()
B.func2()
输出:
A.func1 A
func2 A
B.func1 B
func2 B
6、 mutable & immutable
http://blog.youkuaiyun.com/hsuxu/article/details/7785835
在python里面哪些是immutable的呢?
numbers, strings, tuples, frozensets
(set & frozenset: blog.youkuaiyun.com/fall221/article/details/8478574 )
其实,还有一种特殊情况,就是自定义的类型
一般情况下,程序员自定的python类型都是mutable的,但是如果想定制immutable的数据类型,那么必须要重写object的__setattr__, __delattr__的方法,
class Immutable(object):
def __setattr__(self, *args):
raise TypeError("can't modify the value of immutable instance")
__delattr__ = __setattr__
def __init__(self, value):
super(Immutable, self).__setattr__("value", value)