Python通过len函数返回对象长度

英文文档:

len(s)

Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

  返回对象的长度

说明:  

  1. 返回对象的长度,参数可以是序列(比如字符串、字节数组、元组、列表和range对象),或者是集合(比如字典、集合、不可变集合)

>>> len('abcd') # 字符串
4
>>> len(bytes('abcd','utf-8')) # 字节数组
4
>>> len((1,2,3,4)) # 元组
4
>>> len([1,2,3,4]) # 列表
4
>>> len(range(1,5)) # range对象
4
>>> len({'a':1,'b':2,'c':3,'d':4}) # 字典
4
>>> len({'a','b','c','d'}) # 集合
4
>>> len(frozenset('abcd')) #不可变集合
4

  2. 如果参数为其它类型,则其必须实现__len__方法,并返回整数,否则报错。

>>> class A:
  def __init__(self,name):
    self.name = name
  def __len__(self):
    return len(self.name)

>>> a = A('')
>>> len(a)
0
>>> a = A('Aim')
>>> len(a)
3
>>> class B:
  pass

>>> b = B()
>>> len(b)
Traceback (most recent call last):
 File "<pyshell#65>", line 1, in <module>
  len(b)
TypeError: object of type 'B' has no len()
>>> class C:
  def __len__(self):
    return 'len'

>>> c = C()
>>> len(c)
Traceback (most recent call last):
 File "<pyshell#71>", line 1, in <module>
  len(c)
TypeError: 'str' object cannot be interpreted as an integer

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Python3+selenium配置常见报错解决方案

    第一个坑:'geckodriver' executable needs to be in PATH 1.如果启动浏览器过程中报如下错误 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", li

  • python 服务器运行代码报错ModuleNotFoundError的解决办法

    一.问题描述 一段 Python 代码在本地的 IDE 上运行正常,部署到服务器运行后,出现了 ModuleNotFoundError: No module named 'xxx' 错误. 二.问题原因 在代码中引入了其他文件的包(自己写的包,非 pip 安装的),问题出在 import 那行语句. 错误的原因是因为路径的原因,服务器端的路径和我们本地的路径不一样显示. 三.解决示例 要解决这个问题,可以在自己代码的顶端加入以下代码: import sys import os sys.path.

  • Python selenium环境搭建实现过程解析

    一:自动化了解知识 工具安装 什么样的项目适合做自动化? 自动化测试一般在什么阶段开始实施? 你们公司自动化的脚本谁来维护?如何维护? 自动化用例覆盖率是多少? 自动化的原理 通过 webdriver 模块中的关键字和浏览器驱动以及页面元素定位进行操作达到模拟人工操作的效果 你们公司的自动化流程是如何展开的? 对自动化的业务需求进行评审 对自动化测试的场景进行选择, 测试工具的选择, 在功能用例中摘选出该场景的用例 根据评审后的场景输出自动化用例, 执行测试用例, 定期维护脚本 二.工具安装 安

  • Python Selenium自动化获取页面信息的方法

    1.获取页面title title:获取当前页面的标题显示的字段 from selenium import webdriver import time browser = webdriver.Chrome() browser.get('https://www.baidu.com') #打印网页标题 print(browser.title) #输出内容:百度一下,你就知道 2.获取页面URL current_url:获取当前页面的URL from selenium import webdriver

  • Python Selenium实现无可视化界面过程解析

    无可视化界面的意义 有时候我们爬取网页数据,并不希望看其中的过程,只想看到最后的数据结果就可以了,这时候,***面就很有必要了! 代码如下 from selenium import webdriver from time import sleep #实现无可视化界面 from selenium.webdriver.chrome.options import Options #实现规避检测 from selenium.webdriver import ChromeOptions #实现无可视化界面

  • Python selenium爬取微信公众号文章代码详解

    参照资料:selenium webdriver添加cookie: https://www.jb51.net/article/193102.html 需求: 想阅读微信公众号历史文章,但是每次找回看得地方不方便. 思路: 1.使用selenium打开微信公众号历史文章,并滚动刷新到最底部,获取到所有历史文章urls. 2.对urls进行遍历访问,并进行下载到本地. 实现 1.打开微信客户端,点击某个微信公众号->进入公众号->打开历史文章链接(使用浏览器打开),并通过开发者工具获取到cookie

  • Python中Selenium模块的使用详解

    Selenium的介绍.配置和调用 Selenium(浏览器自动化测试框架) 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Firefox,Safari,Google Chrome,Opera等.这个工具的主要功能包括:测试浏览器的兼容性--测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上.测试系统功能--创建回归测试检验软件功能和用户需求.支持自动录制动作和自动生成 .

  • Python通过len函数返回对象长度

    英文文档: len(s) Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set). 返回对象的长度 说明: 1. 返回对象的长度,参数可以是序列(比如字符串.字节数组.元

  • Python中return函数返回值实例用法

    在学习return函数时候,还是要知道了解它最主要的函数作用,比如,怎么去实现返回一个值,另外还有就是我们经常会用到的使用return能够进行多值输出,这才是我们需要抓住知识的重点,针对上述所提及的内容,都可以来往下看文章,答案都在文章内容获取哦~ return 添加返回值 return 显示返回对象 返回值接受:value = func() 例子:计算学成最高分 listv = [90,80,88,77,66] # 分数计算return高分 def scoreCalculate(values)

  • 解决使用python print打印函数返回值多一个None的问题

    根本原因: python定义函数时,一般都会有指定返回值,如果没有显式指定返回值,那么python就会默认返回值为None 我们输入的代码如下: def test(): print('aaa') print(test()) 相当于执行了: def test(): print('aaa') return None print(test()) 如果不想要有None,那么就要添加返回值 def test(): return 'ccc' print(test()) 补充知识:python中如何实现pri

  • Python API len函数操作过程解析

    在python中除了print函数之外,len函数和type函数应该算是使用最频繁的API了,操作都比较简单. 一.len函数简介 返回对象的长度(项目数)参数可以是序列(例如字符串str.元组tuple.列表list)或集合(例如字典dict.集合set或冻结集合frozenset) 语法: len(s) 参数: s – 对象或者序列(例如字符串str.元组tuple.列表list)或集合(例如字典dict.集合set或冻结集合) 返回值:返回长度(>=0) 二.len函数使用 # !usr/

  • python读取oracle函数返回值

    在oracle中创建一个函数,本来是想返回一个index table的,没有成功.想到文本也可以传输信息,就突然来了灵感,把返回值设置文本格式. 考虑到返回数据量可能会很大,varchar2类型长度吃紧,于是将返回值类型设置为clob.  我是用scott用户的测试表emp,这个是函数定义情况: create or replace function test_query_func(dept varchar2) return clob is type test_record is record (

  • Python如何使用vars返回对象的属性列表

    英文文档: vars([object]) Return the __dict__ attribute for a module, class, instance, or any other object with a __dict__ attribute. Objects such as modules and instances have an updateable __dict__ attribute; however, other objects may have write restri

  • Python通过getattr函数获取对象的属性值

    英文文档: getattr(object, name[, default]) Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object's attributes, the result is the value of that attribute. For example, getattr(x, 'foobar')

  • Python基于callable函数检测对象是否可被调用

    英文文档: callable(object) Return True if the object argument appears callable, False if not. If this returns true, it is still possible that a call fails, but if it is false, calling object will never succeed. Note that classes are callable (calling a c

  • Python入门及进阶笔记 Python 内置函数小结

    内置函数 常用函数 1.数学相关 •abs(x) abs()返回一个数字的绝对值.如果给出复数,返回值就是该复数的模. 复制代码 代码如下: >>>print abs(-100) 100 >>>print abs(1+2j) 2.2360679775 •divmod(x,y) divmod(x,y)函数完成除法运算,返回商和余数. 复制代码 代码如下: >>> divmod(10,3) (3, 1) >>> divmod(9,3) (

  • Python内置函数及功能简介汇总

    python内建函数 最近一直在看python的document,打算在基础方面重点看一下python的keyword.Build-in Function.Build-in Constants.Build-in Types.Build-in Exception这四个方面,其实在看的时候发现整个<The Python Standard Library>章节都是很不错的,其中描述了很多不错的主题.先把Build-in Function罗列一下吧,初学者的了解,分类可能不准确,一起交流. 一.数学运

随机推荐