puzzlement about classmethod
Hi, Consider the following small script. My understanding of how this works is that, conceptually, class B holds a separate copy of variable x from class A. Nearly everything behaves the way I would expect, except that setting x to 12 in A using class_setx at the beginning also sets the value for x in B. However, the converse (setting x in B using class_setx), does not change the value in A, which I would consider to be the expected behavior. However, after that the two x variables appear to behave independently. Can anyone explain to me why this is so? Regards, Faheem Mitha. ** #!/usr/bin/python class A(object): x = 0 def class_getx(cls): return cls.x class_getx = classmethod(class_getx) def class_setx(cls, _x): cls.x = _x class_setx = classmethod(class_setx) def getx(self): return type(self).x def setx(self, _x): type(self).x = _x class B(A): pass def printx(): print "*** begin printing values of x... ***" print "Fetching from A using class_getx gives %s"%(A.class_getx()) print "Fetching from B using class_getx gives %s"%(B.class_getx()) print "Fetching from A using instance a and getx gives %s"%(a.getx()) print "Fetching from B using instance b and getx gives %s"%(b.getx()) print "*** end printing values of x... ***" a = A() b = B() printx() print "setting x to 12 in A using class_setx"; A.class_setx(12) printx() print "setting x to 15 in B using class_setx"; B.class_setx(15) printx() print "setting x to 10 in A using class_setx" A.class_setx(10) printx() print "setting x to 44 in A using instance a and setx"; a.setx(55) printx() print "setting x to 22 in B using instance b and setx"; b.setx(22) printx() -- http://mail.python.org/mailman/listinfo/python-list
regular expression question (re module)
Hi, I need to match a string of the form capital_letter underscore capital_letter number against a string of the form anything capital_letter underscore capital_letter number some_stuff_not_starting with a number Eg D_A1 needs to match with DUKE1_plateD_A1.CEL, but not any of DUKE1_plateD_A10.CEL, Duke1_PlateD_A11v2.CEL, DUKE1_plateD_A12.CEL. Similarly D_A10 needs to match DUKE1_plateD_A10.CEL, but not any of DUKE1_plateD_A1.CEL, Duke1_PlateD_A11v2.CEL, DUKE1_plateD_A12.CEL. Similarly D_A11 needs to match Duke1_PlateD_A11v2.CEL, but not any of DUKE1_plateD_A1.CEL, DUKE1_plateD_A10.CEL, DUKE1_plateD_A12.CEL. Thanks in advance. Please cc me with any reply. Faheem. -- http://mail.python.org/mailman/listinfo/python-list
python string comparison oddity
Hi everybody, I was wondering if anyone can explain this. My understanding is that 'is' checks if the object is the same. However, in that case, why this inconsistency for short strings? I would expect a 'False' for all three comparisons. This is reproducible across two different machines, so it is not just a local quirk. I'm running Debian etch with Python 2.4.4 (the default). Thanks, Faheem. In [1]: a = '--' In [2]: a is '--' Out[2]: False In [4]: a = '-' In [5]: a is '-' Out[5]: True In [6]: a = 'foo' In [7]: a is 'foo' Out[7]: True -- http://mail.python.org/mailman/listinfo/python-list
Re: python string comparison oddity
On Wed, 18 Jun 2008 12:57:44 -0700 (PDT), Lie <[EMAIL PROTECTED]> wrote: > On Jun 19, 2:26 am, Faheem Mitha <[EMAIL PROTECTED]> wrote: >> Hi everybody, >> >> I was wondering if anyone can explain this. My understanding is that 'is' >> checks if the object is the same. However, in that case, why this >> inconsistency for short strings? I would expect a 'False' for all three >> comparisons. This is reproducible across two different machines, so it is >> not just a local quirk. I'm running Debian etch with Python 2.4.4 (the >> default). >> Thanks, Faheem. >> >> In [1]: a = '--' >> >> In [2]: a is '--' >> Out[2]: False >> >> In [4]: a = '-' >> >> In [5]: a is '-' >> Out[5]: True >> >> In [6]: a = 'foo' >> >> In [7]: a is 'foo' >> Out[7]: True > > Yes, this happens because of small objects caching. When small > integers or short strings are created, there are possibility that they > might refer to the same objects behind-the-scene. Don't rely on this > behavior. Yes, but why is '-' and 'foo' cached, and not '--'? Do you know what the basis of the choice is? Faheem. -- http://mail.python.org/mailman/listinfo/python-list