Python笔记

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,classmethod跟运行时的那个cls相关,而staticmethod可以理解为普通模块的函数,只不过被放到了一个class内而已,为这一类class独有。

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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值