


Learn more about the special function __len__(self) in python
len If a class behaves like a list, to get the number of elements, you have to use the len() function. For the len() function to work properly, the class must provide a special method len(), which returns the number of elements.
For example, we write a Students class and pass the name in:
class Students(object): def init(self, *args): self.names = args def len(self): return len(self.names)
As long as the len() method is correctly implemented, you can use the len() function to return the "length" of the Students instance:
>>> ss = Students('Bob', 'Alice', 'Tim')>>> print len(ss)3
Task
The Fibonacci sequence is composed of 0, 1, 1, 2, 3, 5, 8....
Please write a Fib class. Fib(10) represents the first 10 elements of the sequence. print Fib(10) can print out the first 10 elements of the sequence. len(Fib(10)) can correctly return the number of the sequence. 10.
Need to calculate the first N elements of the Fibonacci sequence based on num.
Reference code:
class Fib(object): def init(self, num): a, b, L = 0, 1, [] for n in range(num): L.append(a) a, b = b, a + b self.numbers = L def str(self): return str(self.numbers) repr = str def len(self): return len(self.numbers) f = Fib(10)print fprint len(f)
List can only insert elements through append and insert! ! !
【Related Recommendations】
1. Summary of usage examples of len() function in Python
2. Must master Little knowledge--Detailed explanation of Python len examples
3. An example tutorial on the use of python special class methods
4. Python magic methods __getitem__, __setitem__, __delitem__, __len__ are introduced respectively
The above is the detailed content of Learn more about the special function __len__(self) in python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many website developers face the problem of integrating Node.js or Python services under the LAMP architecture: the existing LAMP (Linux Apache MySQL PHP) architecture website needs...

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Scapy crawler, the reason why pipeline persistent storage files cannot be written? Discussion When learning to use Scapy crawler for data crawler, you often encounter a...

Getting started with Python: Hourglass Graphic Drawing and Input Verification This article will solve the variable definition problem encountered by a Python novice in the hourglass Graphic Drawing Program. Code...

Python process pool handles concurrent TCP requests that cause client to get stuck. When using Python for network programming, it is crucial to efficiently handle concurrent TCP requests. ...

Deeply explore the viewing method of Python functools.partial object in functools.partial using Python...

Choice of Python Cross-platform desktop application development library Many Python developers want to develop desktop applications that can run on both Windows and Linux systems...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...
