[IronPython] Method invocation when assigned to instance differs from default assignment?

Erzengel des Lichtes erzengel-von-licht at cox.net
Sat Feb 17 11:04:18 CET 2007

I encountered an interesting quirk in Python; I want to know what it's about
so that I can understand the expected behavior of a python interpriter. It
occurs in both CPython and IronPython, so I presume it is the intended

Note that I haven't worked with Python very long, and have mostly used
simple scripting that is used to control the game I'm working on, so this
may be well known already, but I found it strange.

Anyway, I noticed when you did the following, the class would make a default
assignment that was referanced in instances:
(This isn't the quirk I'm talking about, it leads to it.)
class MyClass:
  i = 5
Test = MyClass()
Test2 = MyClass()
def PrintTests(Message):
  print Message
  print "Test 1: " + str(Test.i)
  print "Test 2: " + str(Test2.i)
Test.i = 2
PrintTests("Instance Change")
MyClass.i = 4
PrintTests("Class Change")
Test3 = MyClass()
print "Test 3: " + str(Test3.i)

Prints out:
Test 1: 5
Test 2: 5
Instance Change
Test 1: 2
Test 2: 5
Class Change
Test 1: 2
Test 2: 4
Test 3: 4

As you can see, changing the variable on the class type changes it on all
unchanged instances and new instances.

Now, I did this:
class MyClass:
def Method():
  print "hello"
Test = MyClass()
Test.Func1 = Method
MyClass.Func2 = Method

Func1 prints out Hello, but Func2 says "TypeError: Method() takes exactly 0
arguments (1 given)". 

Why does Func1 work? Shouldn't it be passing in Test, and so providing more
arguments than Method wants? Why does it only do this with Func2? Why would
assigning a function to an instance variable cause this behavior to be
different from a class variable? 
Is there any way to have the Func1 behavior, while being able to assign the
change to all unchanged instances and new instances like I can with other
variables? It's not something I really need to do, but I need to know
Python's behavior in order to ensure I reproduce it correctly (My co-workers
could get around any issues, but this is intended for the end user to have
available as well, and having it act differently from CPython, IronPython,
and others would be a Bad ThingT).

Thanks for any info,

Erzengel des Lichtes
Hikari no Daitenshi
Archangel of Light

More information about the Ironpython-users mailing list