Fluent Python学习笔记1

Chapter 1 The Python Data Model

  1. Python data model describes the API that you can use to make your own objects play well with the most idiomatic language features;
  2. The Python interpreter invokes special methods to perform basic object operations,often triggered by special syntax;
    Special methods are meant to be called by the Python interpreter,and not by you;
    Normally,your code should not have many direct calls to special methods;
    The only special method that is frequently called by user code directly id __init__ ,to invoke the initializer of the superclass in your own __init__ implementation.
    If you need to invoke a special method, it is usually better to call the related built-in function(e.g.,len,iter,str,etc).
  3. The special method names are always written with leading and trailing double underscores(i.e.,__getitem__);
    The special methods are also known as dunder methods;
  4. collections.namedtuple Since Python 2.6,namedtuple can be used to build classes of objects that are just bundles of attributes with no custom methods,like a database record.
  5. random.choice gets a random item from a sequence.
  6. abs built-in function returns the absolute value of integers and floats,and the magnitude of complex numbers;
  7. __repr__ special method is called by the repr built-in to get the string representation of the object for inspection;
  8. bool(x) returns True or False;
  9. len(x) runs very fast when x is an instance of a built-in type;
    Because no method is called for the built-in objects of CPython: the length is simply read from a field in a C struct.
  10. By implementing special methods, your objects can behave like the built-in types,enabling the expressive coding style the community considers Pythonic
    (中文大概是:Python化风格);

  11. Python object provides usable string representations of itself,one used for debugging and logging,another for presentation to the end users; That is why the special methods __repr__ and __str__ exist in the data model;
    (__repr__ is for developers, __str__ is for customers)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值