Python uses ordinary objects as dictionary classes (DICT)

Source: Internet
Author: User

Now I know there are two ways to do this:

1 class inheritance Dict classes defined

For example

Class A (dict): Pass

A = A ()

a[' name '] = 12

2 Adding a custom class

__setitem__ () __getitem__ () method

Class A:def __init__ (self, cfg={}): <strong><span style= "color: #ff0000;" >self.cfg = Cfg</span></strong>def __setitem__ (self, Key, value): <span style= "color: #ff0000;" >self.cfg[key] = Value</span>def __getitem__ (self, key): <span style= "color: #ff0000;" >return self.cfg[key]</span>a = A () a[' B '] = 2print (a[' B '])
Output:
2[finished in 0.0s]
This is to add a dictionary class object to the class and store the key,value in it.

In both of these ways, we can use ordinary objects just like dictionary class objects.

The second method is to learn tkinter when the use of this, in the Internet did not search methods, see its source code to find this method.



Python uses ordinary objects as dictionary classes (DICT)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.