Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Python class of operator overloading     - HttpClient Tutorial (Programming)

- SSH automatic disconnection problem solving (Linux)

- Setting Linux desktop environment, achieve HiDPI display support (Linux)

- Docker command Detailed (Linux)

- Construction Spark source and application development environment (Server)

- Linux kernel network subsystem analysis (Programming)

- Graphical development environment to build Android under Ubuntu 11.04 (Linux)

- How to achieve SSH without password login (Server)

- View processes and threads under Linux (Linux)

- Red Hat Linux security settings document (Linux)

- build Android environment on Ubuntu 12.04 (Server)

- Oracle database online redo logs are several methods of recovery of deleted (Database)

- HTML5 Fundamentals study notes (Programming)

- Linux Security trust with users (Linux)

- To create a file in Linux directory by setfacl (Linux)

- Ubuntu 15.10 15.04 14.10 14.04 Install Ubuntu Tweak (Linux)

- Unix average load average load calculation method (Server)

- Android child thread really do not update UI (Programming)

- Linux system started to learn: how to view the Linux thread of a process (Linux)

- Ubuntu Control Panel to resolve network-manager icon display issue (Linux)

  Python class of operator overloading
  Add Date : 2018-11-21      
  Python class of operator overloading, this article is just a summary nature, to be in the future will have time for the more difficult to understand in part to add some examples.

Constructors and destructors


Function call


Printing operation

str (x), print (X): will first try __str__ method, and then try __repr__ method; enter X under repr (X) or interactive mode, it will only try __repr__ methods.

Property operations

__getattr__: not only class attributes defined by the dot operator will try this method
__setattr__: class assignment will be to try all the properties of the method, self.attr = val becomes self.setattr ( 'attr', val)
__getattribute__: class of all properties dot operator will try this method

Index and fragmentation


Iterative environment

In all iterations environment, we will first try __iter__ method, and then try __getitem__ method, but note that there is no overloading iterator index expression, so if you need to while according to the following standard manual iteration, you need to define __getitem__ methods.

Test member

in the members will first try __contains__ test method, then __iter__ method, and finally __getitem__ methods.

Descriptor properties

Here are the main __delete__, also the destructor __del__ is completely different.

Comparison of size

__eq__:! = and == will try this method, it is necessary to ensure that the two operators are properly effect.
Consider the following examples:

class X:
    def __init __ (self, value):
        self.data = value
    def __eq __ (self, value):
        return self.data == value

x = X (4)
print (x == 5)
print (x! = 5)

Math operations

__add__, __iadd__, __radd__
__sub__, __isub__, __rsub__
__mul__, __imul__: multiplication
__neg__, __pos__: indicates -obj, + obj
__floordiv__, __ifloordiv__, __truediv__, __itruediv__
__mod__, __imod__, __imod__
__pow__, __ipow__
a + = b attempts to enhance the addition __iadd__, then try a normal addition __add__
x = b + obj: if b is not an instance, it invokes the right of the adder __radd__
When we added two instances when, Python run __add__, which in turn left by simplifying operand triggered __radd__.

Object arithmetic operations

__concat__: for example the sequence a, b of the addition a + b

Bitwise Operators

__and__, __iand__
__or__, __ior__, __xor__, __ixor__
__inv__, __invert__: ~ obj
__lshift__, __ilshift__, __rshift__, __irshift__
a & = b: first tries __iand__, then try __add__

Boolean test

Boolean test will first try __bool__ a specific method, and then try __len__ methods. Python2.6 in the name of not using __bool__, but __nonzero__

Environmental Management

Mainly used with context management statement.

Used to define the class

This function will call the class initialization function __init __ (before)

Integer value

If the bin (X), hex (X), oct (X), O [X], O [X:] and so on need to pass a number, index, passing an object value slice, then the object __index__ will be called.
It is mainly used to replace Python2.x in __oct__, __hex__
- JDK installation notes under CentOS (Linux)
- Ubuntu install video playback software SMPlayer (Linux)
- Ubuntu 12.04 installation NVIDIA GTX750 graphics driver (Linux)
- RHEL 7.1 compile and install Ganglia 3.7.1 (Server)
- Autojump: an advanced cd command in the Linux file system fast navigation (Linux)
- Install snort intrusion detection system on Debian (Linux)
- Create the container and run the application Docker (Server)
- iptraf: A Practical TCP / UDP network monitoring tools (Linux)
- Linux LVM - File system extension (Linux)
- Ubuntu users to install Xtreme Download Manager (Linux)
- How to set cache valid time in Apache (Server)
- Java string concatenation techniques (StringBuilder tips) (Programming)
- Linux asynchronous read documents by AIO (Programming)
- Difference Redhat5 and 6 YUM source configuration (Linux)
- Installation Elementary OS Freya to do some settings (Linux)
- Use Redis as time-series database: why and how (Database)
- How do you know your public IP terminal in Linux (Linux)
- The basic principles for the protection of a good linux server security (Linux)
- Ubuntu 14.04 Nvidia proprietary drivers for install two graphic cards (Linux)
- JDK comes with tools jinfo (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.