has_key & ContainsKey

October 11, 2007

Python’s built in dictionary type – {} – has a has_key method. C#’s System.Collections.Hashtable has ContainsKey. I’d like the same Python code to operate on dictionaries and hashtables regardless. Fortunately IronPython adds the ContainsKey method to the built in dictionary type, so a simple replacement of has_key invocations with ContainsKey does the trick.


2 Responses to “has_key & ContainsKey”

  1. Although has_key is deprecated in CPython and it is better to use the more readable ‘key in dictionary’ (which uses the ‘__contains__’ method under the hood).

    Looks like ‘ContainsKey’ serves your purposes though.


  2. etrading Says:

    Thanks – didn’t know it was deprecated. Some of my Python idioms are still 1.5.2ish…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s