473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Accessing class variables in staticmethods.

Hi,

I want to access a static variable in a staticmethod. The variable can
be redefined by derived classes and that should be reflected in base's
staticmethod. Consider this trivial example-

class Base:
staticvar = 'Base'

@staticmethod
def printname():
# this doesn't work
# print staticvar
# this does work but derived classes wouldn't behave as I want
print Base.staticvar

class Derived(Base):
staticvar = 'Derived'

Base.printname() # should print 'Base'
Derived.printname() # should print 'Derived'

Any idea on how to go about this? Also from a staticmethod how can I
find out other attributes of the class (not objects)? Do static methods
get some classinfo via some implicit argument(s)?

Thanks in advance,
Ram

Jan 21 '07 #1
2 2932
Sam
On 21 Jan 2007 12:49:17 -0800, Ramashish Baranwal
<ra*************@gmail.comwrote:
class Base:
staticvar = 'Base'

@staticmethod
def printname():
# this doesn't work
# print staticvar
# this does work but derived classes wouldn't behave as I want
print Base.staticvar

class Derived(Base):
staticvar = 'Derived'

Base.printname() # should print 'Base'
Derived.printname() # should print 'Derived'

Any idea on how to go about this? Also from a staticmethod how can I
find out other attributes of the class (not objects)? Do static methods
get some classinfo via some implicit argument(s)?
No, staticmethods get told nothing about the class they're being
defined in. What you want is a classmethod, which gets passed the
class to work with.

Using classmethods, your code becomes:

#untested, bear in mind
class Base:
staticvar = 'Base'

@classmethod
def printname(cls):
print cls.staticvar

class Derived(Base):
staticvar = 'Derived'

Base.printname() #prints 'Base'
Derived.printname() #prints 'Derived'

Incidentally, you can also use cls.__name__ for this purpose, but I
guess that your actual motivation for this is more complicated than
class names.
Jan 21 '07 #2
Sam wrote:
On 21 Jan 2007 12:49:17 -0800, Ramashish Baranwal
<ra*************@gmail.comwrote:
class Base:
staticvar = 'Base'

@staticmethod
def printname():
# this doesn't work
# print staticvar
# this does work but derived classes wouldn't behave as I want
print Base.staticvar

class Derived(Base):
staticvar = 'Derived'

Base.printname() # should print 'Base'
Derived.printname() # should print 'Derived'

Any idea on how to go about this? Also from a staticmethod how can I
find out other attributes of the class (not objects)? Do static methods
get some classinfo via some implicit argument(s)?

No, staticmethods get told nothing about the class they're being
defined in. What you want is a classmethod, which gets passed the
class to work with.

Using classmethods, your code becomes:

#untested, bear in mind
class Base:
staticvar = 'Base'

@classmethod
def printname(cls):
print cls.staticvar

class Derived(Base):
staticvar = 'Derived'

Base.printname() #prints 'Base'
Derived.printname() #prints 'Derived'

Incidentally, you can also use cls.__name__ for this purpose, but I
guess that your actual motivation for this is more complicated than
class names.
Thanks Sam, using classmethod works. You guessed it correctly, my
actual motivation is more complicated but on the same line.

-Ram

Jan 22 '07 #3

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

Similar topics

4
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned...
12
by: Andrew Jaffe | last post by:
Hi, I have a class with various class-level variables which are used to store global state information for all instances of a class. These are set by a classmethod as in the following (in...
2
by: Earl Teigrob | last post by:
I am programming ASP.NET using C#. I have been accessing static variables accross my entire application but now need to change some of the static variables that are session specific to instance...
1
by: Nathan | last post by:
Hi, I have created a class library creating a number of forms and a few public variables. I have a project that references the .dll for this class library, and in that project I need to access...
8
by: dwok | last post by:
I have been wondering this for a while now. Suppose I have a class that contains some private member variables. How should I access the variables throughout the class? Should I use properties that...
5
by: Cyril Gupta | last post by:
Hello, I have a class inside another class. The Scenario is like Car->Engine, where Car is a class with a set of properties and methods and Engine is another class inside it with its own set of...
5
by: TS | last post by:
is it preferred to access member variables directly in code, on the page that declared them, versus going thru a property accessor? I would think that since theres no security concerns or anything...
2
by: Jurek Dabrowski | last post by:
hi all, I have a question in reference to accessing variables in another class maybe someone has dealt with before. I have some public variables declared in my main plug-in class...
26
by: momobear | last post by:
hi, I am puzzled about how to determine whether an object is initilized in one class, anyone could give me any instructions? here is an example code: class coffee: def boil(self): self.temp =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.