虐聽力請進 – Scientific American Podcast

感受一下飛翔~

Python 2.x 中的super()調用需要傳送參數,Python 3.x以後可以省略

Why is super() broken in Python-2.x? – Stack Overflow.

super() is not broken — it just should not be considered the standard way of calling a method of the base class. This did not change with Python 3.x. The only thing that changed is that you don’t need to pass the arguments self, cls in the standard case that self is the first parameter of the current function and cls is the class currently being defined.

Regarding your question when to actually use super(), my answer would be: hardly ever. I personally try to avoid the kind of multiple inheritance that would make super() useful.

Edit: An example from real life that I once ran into: I had some classes defining a run() method, some of which had base classes. I used super() to call the inherited constructors — I did not think it mattered because I was using single inheritance only:

class A(object):def __init__(self, i):
        self.i = i
    def run(self, value):return self.i * value

class B(A):def __init__(self, i, j):
        super(B, self).__init__(i)
        self.j = j
    def run(self, value):return super(B, self).run(value)+ self.j

Just imagine there were several of these classes, all with individual constructor prototypes, and all with the same interface to run().

Now I wanted to add some additional functionality to all of these classes, say logging. The additional functionality required an additional method to be defined on all these classes, say info(). I did not want to invade the original classes, but rather define a second set of classes inheriting from the original ones, adding the info() method and inheriting from a mix-in providing the actual logging. Now, I could not use super() in the constructor any more, so I used direct calls:

classLogger(object):def __init__(self, name):
        self.name = name
    def run_logged(self, value):print"Running", self.name,"with info", self.info()return self.run(value)classBLogged(B,Logger):def __init__(self, i, j):
        B.__init__(self, i, j)Logger.__init__("B")def info(self):return42

Here things stop working. The super() call in the base class constructor suddenly calls Logger.__init__(), and BLogged can’t do anything about it. There is actually no way to make this work, except for removing the super() call in B itself.

[Another Edit: I don’t seem to have made my point, judging from all the comments here and below the other answers. Here is how to make this code work using super():

class A(object):def __init__(self, i,**kwargs):
        super(A, self).__init__(**kwargs)
        self.i = i
    def run(self, value):return self.i * value

class B(A):def __init__(self, j,**kwargs):
        super(B, self).__init__(**kwargs)
        self.j = j
    def run(self, value):return super(B, self).run(value)+ self.j

classLogger(object):def __init__(self, name,**kwargs):
        super(Logger,self).__init__(**kwargs)
        self.name = name
    def run_logged(self, value):print"Running", self.name,"with info", self.info()return self.run(value)classBLogged(B,Logger):def __init__(self,**kwargs):
        super(BLogged, self).__init__(name="B",**kwargs)def info(self):return42

b =BLogged(i=3, j=4)

Compare this with the use of explicit superclass calls. You decide which version you prefer.]

This and similar stories are why I think that super() should not be considered the standard way of calling methods of the base class. It does not mean super() is broken.

Python中的“單下劃線”和“雙下劃線”變量的含義和區別

想要知道Python中的“單下劃線”和“雙下劃線”變量的含義和區別嗎,請看下面的解釋

private – The meaning of a single- and a double-underscore before an object name in Python – Stack Overflow.

 

Single Underscore

Names, in a class, with a leading underscore are simply to indicate to other programmers that the attribute or method is intended to be private. However, nothing special is done with the name itself.

To quote PEP-8:

_single_leading_underscore: weak “internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore.

Double Underscore (Name Mangling)

From the Python docs:

Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, so it can be used to define class-private instance and class variables, methods, variables stored in globals, and even variables stored in instances. private to this class on instances of other classes.

And a warning from the same page:

Name mangling is intended to give classes an easy way to define “private” instance variables and methods, without having to worry about instance variables defined by derived classes, or mucking with instance variables by code outside the class. Note that the mangling rules are designed mostly to avoid accidents; it still is possible for a determined soul to access or modify a variable that is considered private.

Example

>>>classMyClass():...def __init__(self):...self.__superprivate ="Hello"...self._semiprivate =", world!"...>>> mc =MyClass()>>>print mc.__superprivate
Traceback(most recent call last):File"<stdin>", line 1,in<module>AttributeError: myClass instance has no attribute '__superprivate'>>>print mc._semiprivate
, world!>>>print mc.__dict__
{'_MyClass__superprivate':'Hello','_semiprivate':', world!'}

 

Python Interview Question and Answers

from http://ilian.i-n-i.org/python-interview-question-and-answers/

Python Interview Question and Answers

For the last few weeks I have been interviewing several people for Python/Django developers so I thought that it might be helpful to show the questions I am asking together with the answers. The reason is … OK, let me tell you a story first.
I remember when one of my university professors introduced to us his professor – the one who thought him. It was a really short visit but I still remember one if the things he said. “Ignorance is not bad, the bad thing is when you do no want to learn.”
So back to the reason – if you have at least taken care to prepare for the interview, look for a standard questions and their answers and learn them this is a good start. Answering these question may not get you the job you are applying for but learning them will give you some valuable knowledge about Python.
This post will include the questions that are Python specific and I’ll post the Django question separately.

  1. How are arguments passed – by reference of by value?
    The short answer is “neither”, actually it is called “call by object” or “call by sharing”(you can check here for more info). The longer one starts with the fact that this terminology is probably not the best one to describe how Python works. In Python everything is an object and all variables hold references to objects. The values of these references are to the functions. As result you can not change the value of the reference but you can modify the object if it is mutable. Remember numbers, strings and tuples are immutable, list and dicts are mutable.
  2. Do you know what list and dict comprehensions are? Can you give an example?
    List/Dict comprehensions are syntax constructions to ease the creation of a list/dict based on existing iterable. According to the 3rd edition of “Learning Python” list comprehensions are generally faster than normal loops but this is something that may change between releases. Examples:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # simple iteration
    a = []
    for x in range(10):
        a.append(x*2)
    # a == [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
    # list comprehension
    a = [x*2 for x in range(10)]
    # dict comprehension
    a = {x: x*2 for x in range(10)}
    # a == {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}
  3. What is PEP 8?
    PEP 8 is a coding convention(a set of recommendations) how to write your Python code in order to make it more readable and useful for those after you. For more information check PEP 8.
  4. Do you use virtual environments?
    I personally and most(by my observation) of the Python developers find the virtual environment tool extremely useful. Yeah, probably you can live without it but this will make the work and support of multiple projects that requires different package versions a living hell.
  5. Can you sum all of the elements in the list, how about to multuply them and get the result?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    # the basic way
    s = 0
    for x in range(10):
        s += x
    # the right way
    s = sum(range(10))
    # the basic way
    s = 1
    for x in range(1, 10):
        s = s * x
    # the other way
    from operator import mul
    reduce(mul, range(1, 10))

    As for the last example, I know Guido van Rossum is not a fan of reduce, more info here, but still for some simple tasks reduce can come quite handy.

  6. Do you know what is the difference between lists and tuples? Can you give me an example for their usage?
    First list are mutable while tuples are not, and second tuples can be hashed e.g. to be used as keys for dictionaries. As an example of their usage, tuples are used when the order of the elements in the sequence matters e.g. a geographic coordinates, “list” of points in a path or route, or set of actions that should be executed in specific order. Don’t forget that you can use them a dictionary keys. For everything else use lists.
  7. Do you know the difference between range and xrange?
    Range returns a list while xrange returns a generator xrange object which takes the same memory no matter of the range size. In the first case you have all items already generated(this can take a lot of time and memory) while in the second you get the elements one by one e.g. only one element is generated and available per iteration. Simple example of generator usage can be find in the problem 2 of the “homework” for my presentation Functions in Python
  8. Tell me a few differences between Python 2.x and 3.x
    There are many answers here but for me some of the major changes in Python 3.x are: all strings are now Unicode, print is now function not a statement. There is no range, it has been replaced by xrange which is removed. All classes are new style and the division of integers now returns float.
  9. What are decorators and what is their usage?
    According to Bruce Eckel’s Introduction to Python Decorators “Decorators allow you to inject or modify code in functions or classes”. In other words decorators allow you to wrap a function or class method call and execute some code before or after the execution of the original code. And also you can nest them e.g. to use more than one decorator for a specific function. Usage examples include – logging the calls to specific method, checking for permission(s), checking and/or modifying the arguments passed to the method etc.
  10. The with statement and its usage.
    In a few words the with statement allows you to executed code before and/or after a specific set of operations. For example if you open a file for reading and parsing no matter what happens during the parsing you want to be sure that at the end the file is closed. This is normally achieved using the try… finally construction but the with statement simplifies it usin the so called “context management protocol”. To use it with your own objects you just have to define __enter__ and __exit__ methods. Some standard objects like the file object automatically support this protocol. For more information you may check Understanding Python’s “with” statement.

Well I hope this will be helpful, if you have any question or suggestion feel free to comment.

Update: Due to the lots of comments on Reddit and LinkedIn, I understood that there is some misunderstanding about the post. First, the questions I have published are not the only ones I ask, the interview also includes such related to general programming knowledge and logical thinking. Second the questions above help me get a basic understanding of your Python knowledge but they are not the only think that makes my decision. Not answering some of them does not mean that you won’t get the job, but it may show me on which parts we should to work.

The 2 Biggest Mistakes I Made When Learning to Code

from http://www.suneelius.com/2012/09/07/the-2-biggest-mistakes-i-made-when-learning-to-code/

 

One year ago, I was fresh out of college and determined to take on the real world. I had so many startup ideas that I wanted to bring to life. But, I didn’t know how to code.

Following the advice of Vin Vacanti , Yipit co-founder, I began to teach myself to code.

While I eventually learned enough to build my own prototypes, it was a rocky road. If you’re on the same path, I hope you can avoid my mistakes.

Mistake #1: I spent too much time learning things that I didn’t actually need

I had no clue where to even begin. There’s just so much out there , so many buzz words and so many competing viewpoints that it was hard to get a sense of what was important. After trawling Hacker News, Quora, and StackOverflow and randomly pulling terms from the most popular articles, I came up with this crazy, hodgepodge list:

HTML, CSS, AJAX, PHP, Javascript, Heroku, Celery, SQL, jQuery, Django, POSTGRES, nodeJS, BackboneJS, Ruby, Rails, MongoDB, Python

I didn’t know which components I needed and which ones I didn’t, so I just tried to learn all of them.

I should’ve narrowed down the list to only what I needed for a working prototype.

Ultimately, I figured out what everything in the list actually did and reduced it to:

  1. HTML: structures a page’s content (e.g., this is a link; that is a title).
  2. CSS: styles the content (e.g., makes a link turn bold when you hover over it).
  3. Javascript: makes the content dynamic (e.g., launches a popup when you click a link).
  4. jQuery: a Javascript library with easy-to-use plugins (e.g., a picture slideshow).
  5. Python: processes the data (e.g., pulls users’ purchase histories from a database and recommends products they might like).
  6. Django: a web framework written in Python that connects all the above pieces and provides a lot of built-in, necessary functionality (e.g., a readymade login system and an easy way to access any kind of database using just Python). Use a framework. It makes your life easy!

Mistake #2: I didn’t start coding right away.

I spent too much time reading coding books. I had no assurance that what I was reading would actually be useful for my project. Worst of all, because I wasn’t applying what I was reading, nothing even stuck with me.

I should’ve learned by working on small projects from the very beginning.

  1. Actively take some tutorials and code the examples. David Sinsky, a fellow Yipit developer, has some amazing suggestions.
  2. Pick an easy project and code it. Try creating a simple blog or a basic polling app.
  3. Repeat #2 a few times.
  4. Build your startup prototype.

There are so many benefits to jumping right in. You’ll quickly get over any fear you may have of programming. You’ll start seeing the fruits of your labor right away. Most importantly, you’ll rapidly develop the skills you need to build your product.

郭美美与红会被连抛重磅内幕 共有三个干爹

from http://www.popyard.com/cgi-mod/newspage.cgi?num=1406893&r=0&v=0&j=0

 

郭美美与中国红十字会扯不清的关系,在网络上被评为第五大娱乐新闻。网上一个刚注册不久的人,接连抛出郭美美与红会的重磅内幕消息,很多人看后称出自行家之手,揭开了郭美美发迹之路,并盘点了郭美美攀上了几位“干爹”,及中国红会的部份黑幕。

点击图片看原样大小图片





郭美美与红会展开第二轮回合

四川芦山地震救灾引发郭美美中国红十字会的第二轮大战,来势之勐,令外界瞠目,尤其红会内部因是否再查郭美美而公开分裂,红会社监会称要再查郭美美重建红会信誉,红十字领导称没有人要再查郭美美,而期间网络又传闻郭美美拥有致命的性爱录像及红会贪污证据,再掀风暴。

中国红十字会常务副会长赵白鸽不得不出面承诺:“如果两到三年,还是不能翻转‘黑十字’印象的话,我自动请求辞职!” 有评论认为别说3年扭转红十字会的黑印象,30年能不能做到,都还是疑问呢!

郭美美经过整容 开始三流艺人生涯

当年的2011年6月21日,微博上一个名叫“郭美美Baby”的炫富女其认证身份是“中国红十字会商业总经理”,真实身份众说纷纭,此女奢华生活曝光将红十字会推上了舆论的风口浪尖。

据微博最新爆料称,郭美美原名郭美玲,出生湖南益阳,父母离异后,跟母姓,其母真名为郭登峰。郭美美曾就读湖南涉外经济学院,后跟母亲去深圳工作,早期在 深圳东门卖衣服,做过淘宝,还曾是深圳几个酒吧的坐台女。郭美美整容后成为现在的模样,开始三流艺人生涯,后来认识天略集团董事长丘振良,被其包养,也成 为郭美美的第一位“干爹”。

据大陆公开资料显示,拥有20余家境内外企业的天略集团,公司总资产超过20亿元人民币,年营业额近30亿元。

据报料人介绍,中国红十字会下面有个商业红十字会,这个商业红会指定深圳的天略集团做劝募。募捐成功双方会分成,郭美美当然也会有好处。

据大陆“每日经济新闻”在2011年6月24日报导证实,“郭美美炫富门背后:天略集团确曾与红十字会合作”。

该媒体记者在实地采访了天略集团并获悉,天略公司曾于2007年与中国商业系统红十字会有过合作,合作的内容是天略公司募集款项,为西部的学生买保险。

该 媒体还发现商业系统红十字会就是当时新浪认证微博爆料里所说的天略集团的合作机构,其共同合作的项目为“红十字校园安全行动”,在中国红十字会网上该网页 无法打开,仅有该项目的图片链接。《三晋都市报》曾报导,2007年,“校园安全山西行”启动,天略集团将20亿元保险捐赠给陕西省教育厅。也有报导披露 天略集团首期在山东也赠送了10亿元的保险。

报导还称当时新浪认证的微博爆料:郭美美南下深圳并整容开始艺人生涯,认识天略集团董事长丘振良,关系不明。丘与红十字会郭长江相识,郭美玲与郭长江关系不明。

而微博最新曝料称郭美美通过丘振良认识红会的郭长江,丘借郭长江成立某红会商会,假红会公益性质开始垄断车体医疗药品器械广告业务。

当时《每日经济新闻》记者披露:熟悉中国红十字会的人士表示,此项目当时只是一个想法,一直没有实现。郭美美只是说出了原来的构想而已,并没有现实意义。

郭美美第二位“干爹” 地产界大亨

报料称郭美美第二位“干爹”是北京一家着名投资集团的董事长戚XX,曾经开创了高档且罕见的渡假村。

网 络至今流传这样一个故事,2010年3月16日,戚XX、郭美玲、黄迪三人乘坐CA1815航班离京;2010年3月18日,戚XX、郭美玲、黄迪三人乘 坐MF8130航班返京。有证据表明,这三人是一起出去,无其他人员跟随一起。后郭美美炫富事件后,已删除和未删除的微博上有这段时间的照片,显示是在厦 门。不过关于这部份内容目前并不多,也无法证实。

郭美美的第三位干爹:中红博爱公司前董事王军

近日有报料人介绍,王军和几个 商人,策划了一个用黑十字会平台,把3千万做出30亿的项目,这其中需要部级官员的帮助,于是王军找到经纪人,联系到郭美美,由郭美美出人,王军出生活 费,项目启动后,按事先约定,送一部玛莎拉蒂,为了掩人耳目,用郭美美的名字登记。在北京,如果部长打招唿,郭美美再搞个牌子应该不是什么问题。事情万一 败露,跟领导也沾不上边,最多是王军养小三,并不指向背后部长。

中红博爱的项目思路,第一免税;第二,终端设备的投入有人出钱,终端设备的维护有国家财政支出;第三,没有进场费,不需跟物业分成。从社区、学校、医科下手,然后商业、物业,最后进攻写字楼。用捐款箱跟广告机联合挣钱。

报料人微博称,只是当中红博爱的办公室还在装修期间,事情被曝光了,并认为大领导不敢插手了,这个项目因此胎死腹中。报料人认为整个事件中,郭美美只是其中的一个环节,此案需要关注的核心是权力资本套利。

而 这些大陆媒体二年前也曾部份披露证实。据新京报报导,商业红会关联公司中红博爱公司法人翁涛2011年7月3日表示,郭美美男友是该公司前董事王军。炫富 事件发生后,王军已辞去董事职务。翁涛表示王军是生意人,是他的朋友,并持有深圳物华公司10%股份,而物华公司持有中红博爱公司60%股份。同时,王军 承认郭美美在网上炫耀的名车名包都是他所赠。

报导还说,中红博爱资产管理有限公司是2008年,由王鼎公司和北京圣华杰广告传媒公司出资5千万元成立,是红十字博爱服务站进社区项目实施方。

据中国经营报报导,2011年8月3日郭美美母女接受了郎咸平独家专访。在采访中,郭美美称中红博爱的王军是“干爸”。其在网上炫耀的豪车,一部玛莎拉蒂是王军送的,另一部Mini-cooper是其母亲送给她的18岁生日礼物。

据称,王军是中红博爱的挂名董事,公司总投资是六七千万,他投资了一千多万。但因为公司尚未启动,王军并没有从中红博爱挣到钱。

增大肌肉塊的14大秘密

 

 

 增大肌肉块的14大秘诀:大重量、低次数、多组数、长位移、慢速度、高密度、念动一致、顶峰收缩、持续紧张、组间放松、多练大肌群、训练后进食蛋白质、休息48小时、宁轻勿假。

 

 

1. 大重量、低次数:健美理论中用RM表示某个负荷量能连续做的最高重复次数。比如,练习者对一个重量只能连续举起5次,则该重量就是5RM。研究表 明:1-5RM的负荷训练能使肌肉增粗,发展力量和速度;6-10RM的负荷训练能使肌肉粗大,力量速度提高,但耐力增长不明显;10-15RM的负荷训 练肌纤维增粗不明显,但力量、速度、耐力均有长进;30RM的负荷训练肌肉内毛细血管增多,耐久力提高,但力量、速度提高不明显。可见,5-10RM的负 荷重量适用于增大肌肉体积的健美训练。

2. 多组数:什么时候想起来要锻炼了,就做上2~3组,这其实是浪费时间,根本不能长肌肉。必须专门抽出60~90分钟的时间集中锻炼某个部位,每个动 作都做8~10组,才能充分刺激肌肉,同时肌肉需要的恢复时间越长。一直做到肌肉饱和为止,“饱和度”要自我感受,其适度的标准是:酸、胀、发麻、坚实、 饱满、扩张,以及肌肉外形上的明显粗壮等。

3. 长位移:不管是划船、卧推、推举、弯举,都要首先把哑铃放得尽量低,以充分拉伸肌肉,再举得尽量高。这一条与“持续紧张”有时会矛盾,解决方法是快速地通过“锁定”状态。不过,我并不否认大重量的半程运动的作用。

4. 慢速度:慢慢地举起,在慢慢地放下,对肌肉的刺激更深。特别是,在放下哑铃时,要控制好速度,做退让性练习,能够充分刺激肌肉。很多人忽视了退让性练习,把哑铃举起来就算完成了任务,很快地放下,浪费了增大肌肉的大好时机。

5. 高密度:“密度”指的是两组之间的休息时间,只休息1分钟或更少时间称为高密度。要使肌肉块迅速增大,就要少休息,频繁地刺激肌肉。“多组数”也是建立在“高密度”的基础上的。锻炼时,要象打仗一样,全神贯注地投入训练,不去想别的事。


6. 念动一致:肌肉的工作是受神经支配的,注意力密度集中就能动员更多的肌纤维参加工作。练某一动作时,就应有意识地使意念和动作一致起来,即练什么就想什么肌肉工作。例如:练立式弯举,就要低头用双眼注视自已的双臂,看肱二头肌在慢慢地收缩。


7. 顶峰收缩:这是使肌肉线条练得十分明显的一项主要法则。它要求当某个动作做到肌肉收缩最紧张的位置时,保持一下这种收缩最紧张的状态,做静力性练习,然后慢慢回复到动作的开始位置。我的方法是感觉肌肉最紧张时,数1~6,再放下来。

8. 持续紧张:应在整个一组中保持肌肉持续紧张,不论在动作的开头还是结尾,都不要让它松弛(不处于“锁定”状态),总是达到彻底力竭。

9. 组间放松:每做完一组动作都要伸展放松。这样能增加肌肉的血流量,还有助于排除沉积在肌肉里的废物,加快肌肉的恢复,迅速补充营养。

10. 多练大肌群:多练胸、背、腰臀、腿部的大肌群,不仅能使身体强壮,还能够促进其他部位肌肉的生长。有的人为了把胳膊练粗,只练胳膊而不练其他部 位,反而会使二头肌的生长十分缓慢。建议你安排一些使用大重量的大型复合动作练习,如大重量的深蹲练习,它们能促进所有其他部位肌肉的生长。这一点极其重 要,可悲的是至少有90%的人都没有足够重视,以致不能达到期望的效果。因此,在训练计划里要多安排硬拉、深蹲、卧推、推举、引体向上这5个经典复合动 作。

11. 训练后进食蛋白质:在训练后的30~90分钟里,蛋白质的需求达高峰期,此时补充蛋白质效果最佳。但不要训练完马上吃东西,至少要隔20分钟。

12. 休息48小时:局部肌肉训练一次后需要休息48~72小时才能进行第二次训练。如果进行高强度力量训练,则局部肌肉两次训练的间隔72小时也不 够,尤其是大肌肉块。不过腹肌例外,腹肌不同于其他肌群,必须经常对其进行刺激,每星期至少要练4次,每次约15分钟;选三个对你最有效的练习,只做3 组,每组20—25次,均做到力竭;每组间隔时间要短,不能超过1分钟。

13. 宁轻勿假:这是一个不是秘诀的秘诀。许多初学健美的人特别重视练习重量和动作次数,不太注意动作是否变形。健美训练的效果不仅仅取决于负重的重量 和动作次数,而且还要看所练肌肉是否直接受力和受刺激的程度。如果动作变形或不到位,要练的肌肉没有或只是部分受力,训练效果就不大,甚至出偏差。事实 上,在所有的法则中,动作的正确性永远是第一重要的。宁可用正确的动作举起比较轻的重量,也不要用不标准的动作举起更重的重量。不要与人攀比,也不要把健 身房的嘲笑挂在心上。

时事评论员伍凡:2014年中国大崩溃? 【伍凡评论】中共面临2014全面崩溃 ?

时事评论员伍凡:2014年中国大崩溃? | soundofhope.org.

【伍凡评论】中共面临2014全面崩溃

Previous Older Entries