Unexpected __metaclass__ method behavior
Dear fellow Pythonians, I just stumbled upon the following unexpected behavior: class TestType(type): def Foo(self): return 'TestType Foo' class Test(object): __metaclass__ = TestType def Foo(self): return 'Test Foo' t = Test() print t.Foo() print Test.Foo() This will produce: Test Foo Traceback (most recent call last): File "test.py", line 8, in print Test.Foo() TypeError: unbound method Foo() must be called with Test instance as first argument (got nothing instead) I can imagine why this is happening, and that there is no easy solution, but it is not what I was expecting. Anybody willing to explain the details of what's exactly going on during the method lookup of Test.Foo? Kind regards, Sebastian -- http://mail.python.org/mailman/listinfo/python-list
Re: Unexpected __metaclass__ method behavior
Well, you see, I have some database functions that deal with "things" which are either classes or instances thereof. I though polymorphism would be a nice way to handle them identically, like: def do(thing): thing.Foo() do(t) do(Test) But never mind, I now understand that Test.__dict__ can contain only one entry for 'Foo', and that this must be matched. Kind regards, Sebastian -- http://mail.python.org/mailman/listinfo/python-list
What is the encoding of __file__?
Dear all, can someone quickly tell me what the encoding of __file__ is? I can't find it in the documentation. BTW, I'm using Python 2.5.1 on WIndows XP and Vista. Kind regards, Sebastian -- http://mail.python.org/mailman/listinfo/python-list
Re: What is the encoding of __file__?
On 7 Jan., 23:06, "Martin v. Löwis" <[EMAIL PROTECTED]> wrote: > > can someone quickly tell me what the encoding of __file__ is? I can't > > find it in the documentation. > > > BTW, I'm using Python 2.5.1 on WIndows XP and Vista. > > It's platform-specific - the same encoding that is used for file names > (i.e. sys.getfilesystemencoding()). On Windows, it will be "mbcs", which > in turn is installation-specific - on Western European/US installations, > it's "windows-1252". Thanks, I'll then use sys.getfilesystemencoding() to decode _file__ and re-encode into utf-8, which is the default encoding of all strings in our software, as we deal a bit with Chinese terms. Windows-1252 on my box. I just created a directory containing Chinese characters (on Vista), and whoa, files opened with IDLE are empty, import doesn't find modules in that directory. Of course Windows-1252 can't encode these ... But I understand that Python 3 will clean this up? Kind regards, Sebastian -- http://mail.python.org/mailman/listinfo/python-list
Inconsistency of special class method lookup?
Folks, I'm running into the following issue. A staticmethod of a class seems not to be accepted as a special class method of the class object itself. For example: class Foo(object): def __len__(): return 2 __len__ = staticmethod(__len__) print len(Foo) >>> Traceback (most recent call last): File "C:/Dokumente und Einstellungen/All Users/Dokumente/foo.py", line 4, in ? print len(Foo) TypeError: len() of unsized object However, the following works: class FooType(type): def __len__(self): return self.l() class Foo(object): __metaclass__ = FooType def l(): return 3 l = staticmethod(l) print len(Foo) >>> 3 Any good reason why the lookup process doesn't find __len__ as staticmethod of the class? Regards, Sebastian (posting using the account of my wife) -- http://mail.python.org/mailman/listinfo/python-list
Re: Inconsistency of special class method lookup?
Thanks. -- http://mail.python.org/mailman/listinfo/python-list