473,583 Members | 3,097 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

puzzlement about classmethod


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

However, after that the two x variables appear to behave
independently. Can anyone explain to me why this is so?

Regards, Faheem Mitha.

*************** *************** *************** *************** ******

class A(object):
x = 0
def class_getx(cls) :
return cls.x
class_getx = classmethod(cla ss_getx)
def class_setx(cls, _x):
cls.x = _x
class_setx = classmethod(cla ss_setx)
def getx(self):
return type(self).x
def setx(self, _x):
type(self).x = _x

class B(A):

def printx():
print "*** begin printing values of x... ***"
print "Fetching from A using class_getx gives %s"%(A.class_ge tx())
print "Fetching from B using class_getx gives %s"%(B.class_ge tx())
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()
print "setting x to 12 in A using class_setx"; A.class_setx(12 )
print "setting x to 15 in B using class_setx"; B.class_setx(15 )
print "setting x to 10 in A using class_setx"
A.class_setx(10 )
print "setting x to 44 in A using instance a and setx"; a.setx(55)
print "setting x to 22 in B using instance b and setx"; b.setx(22)
Jun 24 '06 #1
1 1118
Faheem Mitha wrote:
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

However, after that the two x variables appear to behave
independently. Can anyone explain to me why this is so?

When you access a class variable B.x before it has been set in B then you
see the value from the base class (A.x). Once you have set a separate value
in the subclass that takes precedence.

Similarly accessing an instance variable b.x first looks in the instance
(b), then in the class (B), then in the base class or classes (here A). The
lookup stops as soon as it finds a value in any of these locations.
Jun 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Karl Chen | last post by:
Hi. Is it possible to make a classmethod out of __getitem__? I.e. I want to do: class C: def __getitem__(p): return 1+p
by: Neil Zanella | last post by:
Hello, In Python, classes are objects. But there is no way to custom print a class object. This would require some syntax such as the one commented out below: With the current "foo = classmethod(foo)" mechanism custom printing for class objects is not possible. #!/usr/bin/python class Foo:
by: C Gillespie | last post by:
Hi, Does anyone know of any examples on how (& where) to use staticmethods and classmethods? Thanks Colin
by: Giovanni Bajo | last post by:
Hello, what's the magic needed to reuse the base-class implementation of a classmethod? class A(object): @classmethod def foo(cls, a,b): # do something pass
by: Laszlo Zsolt Nagy | last post by:
Hello, Is it possible to tell, which instance was used to call the classmethod that is currently running? Background: I have a class called DatabaseConnection and it has a classmethod called process_create_tables. This method should create some database tables defined by a database definition object. The DatabaseConnection has many...
by: andychambers2002 | last post by:
Hi, I'm trying to write a method that needs to know both the class name and the instance details class A: @classmethod def meth(cls, self): print cls
by: james_027 | last post by:
hi, python's staticmethod is the equivalent of java staticmethod right? with classmethod, I can call the method without the need for creating an instance right? since the difference between the two is that classmethod receives the class itself as implicti first argument. From my understanding classmethod are for dealing with class...
by: Matthew Keene | last post by:
I would like to be able to call a specific classmethod on a class name that is going to be passed from another parameter. In other words, I have a call that looks something like: x = Foo.bar() and I would like to generalise this so that I can make this call on any particular class which provides the bar classmethod. I have implemented...
by: Scott SA | last post by:
Hi, I'm using the @classemethod decorator for some convenience methods and for some reason, either mental block or otherwise, can't seem to figure out howto elegantly detect if the call is from an instance or not. Here's the problem: Within the class definition, 'isinstance' has nothing to compare to because the class does not appear to...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.