Importing Static Methods with IPy

Like .NET, Python uses namespaces to avoid name collisions. However, the semantics are a bit different. If you want to use a type or function from a a given namespace in Python, you have to import it into your current scope. For example, if you want to use the Python datetime built-in module, you would import it into the current scope and use it like this:

import datetime
bush_last_day = datetime.date(2009,1,20)

Notice that when I import a Python module this way, it’s scoped into it’s namespace, which forces me to use the entire namespace scoped name to access the type. Of course, that gets tedious quickly, so Python provides a way to import a type from a specific namespace into your current scope like this:

from datetime import date
bush_last_day = date(2009,1,20)

With IronPython, you can do import .NET namespaces as well. Here’s that same code using the standard .NET DateTime class.

from System import DateTime
bush_last_day = DateTime(2009,1,20)

What I didn’t know is that you can import static methods & properties from .NET types into the current scope using the same syntax. Here’s an example:

from System.DateTime import Now

if Now >= bush_last_day:
    print 'celebrate'
else:
    print (bush_last_day - Now).Days, 'days left'

Being able to import a static method into the current scope is pretty convenient. Thanks to my teammate Jimmy for cluing me into this IPy feature.

One caveat though: in Python, you can import an entire namespace into your current scope. You can do that with .NET namespaces, but not with .NET types

from datetime import *         # this works

from System import *           # so does this

from System.DateTime import *  # this doesn’t work

Update: Michael Foord pointed out that if you import Now as I describe above, it places a DateTime object representing the time you imported it into local scope, rather than placing the underlying get_Now static method in local scope. So while DateTime.Now always returns a new value, Now never changes. Sounds like an IPy bug to me, but I’ll have to circle back with the team to be sure.

Morning Coffee 162

  • Another nice thing about the new job: I’m working in the vicinity of some good friends. I was over in building 42 yesterday and made it a point to stop by Pat Helland's office yesterday and spend an hour or so chatting about the new gig. Pat is down the hall from David Hill, whom I worked with on Architecture Strategy. Back in my building, we’re down the hall from the VSX folks including my friends Ken Levy and Gareth Jones. I’m sure there are more folks I know around, but hey it’s only my second week!
  • I’m a big fan of Carbonite, which I use to back up all the digital media on my home computer. With two little kids, we have lots of digital photos as you might imagine . However, one thing that bugs me about Carbonite is that it doesn’t back up video files by default, you have to go in on a folder by folder basis and select “‘Back up Video files in this folder” from the context menu. Given how much trouble this “feature” has given me, I imagine less techie folks don’t even realize their video files aren’t getting backed up. However, I will say the latest version of the Carbonite Software at least makes it easy to find files that aren’t backed up. A quick sweep revealed around a dozen folders that had un-backed-up video files in them, which I promptly fixed.
  • The big news yesterday was the new Google App Engine, which looks to give you access to virtualized infrastructure that sounds similar to what GOOG is rumored to use internally. I like Dave Winer’s comment that this enables “shrinkwrap net apps that scale that can be deployed by civillians.” Given Google’s history w/ Python – Python’s BDFL Guido van Rossum works there – it’s no surprise that Google App Engine (GAE?) runs on Python, though apparently they “look forward to supporting more languages in the future”. I’m guessing “more languages” == Ruby, maybe Erlang too.
  • I wonder if/how Google App Engine will affect Ruby on Rails momentum? If there’s a significant lag before App Engine supports Ruby, will that drive developers to Python web stacks like Django? (Django is included in “the box” with App Engine)?@ PyCon, I was surprised at the intra-language animosity I observed. I wonder how many Python developers are secretly hoping Google never ships Ruby support. I highly doubt Google would do that – they want to tap the exploding RoR market like everyone else – but I’d bet it would really take the wind out of Rails’ sails if they did.
  • Today’s Michael Foord Link: Embedding IronPython 2, Examples of the DLR Hosting API. You can read the DLR Hosting spec, but it’s pretty out of date so Michael’s article helps fill in some of the gaps.
  • Looks like PowerShell has gotten the open source community treatment in a project called Pash. While I’m sure others are excited about PS on Linux or Mac, I’m excited to see PS running on Compact Framework. I wonder if it would work with XNA?
  • Speaking of XNA, XNA Console is a new CodePlex project that provides an IPy console to manipulate your XNA based game on the fly. Python is no stranger to game development – Civ IV for example provided mod capabilities via python. Alas, the compact framework can’t run IPy today, so neither can XNA on Xbox. But wouldn’t it be cool to hack your game in IPy running on a 360 using the messenger kit? (via IPy URLs)
  • Bart De Smet gets functional, writing type switch and pattern matching in C# 3.0. I guess it works, but it sure is ugly. Why not just use F# and be done with it?
  • Soma announces that the VC++ Feature Pack has shipped. Somewhere, I assume, there is much (some?) rejoicing.