|
INTERVIEW QUESTIONS
WEB
PYTHON
DETAILS
Question: How do I create static class data and static class methods?
Answer: Static data (in the sense of C++ or Java) is easy; static methods (again in the sense of C++ or Java) are not supported directly.
For static data, simply define a class attribute. To assign a new value to the attribute, you have to explicitly use the class name in the assignment:
class C: count = 0 # number of times C.__init__ called
def __init__(self): C.count = C.count + 1
def getcount(self): return C.count # or return self.count
c.count also refers to C.count for any c such that isinstance(c, C) holds, unless overridden by c itself or by some class on the base-class search path from c.__class__ back to C.
Caution: within a method of C, an assignment like self.count = 42 creates a new and unrelated instance vrbl named "count" in self's own dict. Rebinding of a class-static data name must always specify the class whether inside a method or not:
C.count = 314
Static methods are possible when you're using new-style classes:
class C: def static(arg1, arg2, arg3): # No 'self' parameter! ... static = staticmethod(static)
However, a far more straightforward way to get the effect of a static method is via a simple module-level function:
def getcount(): return C.count
If your code is structured so as to define one class (or tightly related class hierarchy) per module, this supplies the desired encapsulation.
|
|
|
Category |
Python Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.2) By 9499 users |
Added on |
9/23/2014 |
Views |
71790 |
Rate it! |
|
|
Question:
How do I create static class data and static class methods?
Answer:
Static data (in the sense of C++ or Java) is easy; static methods (again in the sense of C++ or Java) are not supported directly.
For static data, simply define a class attribute. To assign a new value to the attribute, you have to explicitly use the class name in the assignment:
class C: count = 0 # number of times C.__init__ called
def __init__(self): C.count = C.count + 1
def getcount(self): return C.count # or return self.count
c.count also refers to C.count for any c such that isinstance(c, C) holds, unless overridden by c itself or by some class on the base-class search path from c.__class__ back to C.
Caution: within a method of C, an assignment like self.count = 42 creates a new and unrelated instance vrbl named "count" in self's own dict. Rebinding of a class-static data name must always specify the class whether inside a method or not:
C.count = 314
Static methods are possible when you're using new-style classes:
class C: def static(arg1, arg2, arg3): # No 'self' parameter! ... static = staticmethod(static)
However, a far more straightforward way to get the effect of a static method is via a simple module-level function:
def getcount(): return C.count
If your code is structured so as to define one class (or tightly related class hierarchy) per module, this supplies the desired encapsulation. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
How can I organize my code to make it easier to change the base class?
|
View Answer
|
|
How do I call a method defined in a base class from a derived class that overrides it?
|
View Answer
|
|
I want to do a complicated sort: can you do a Schwartzman Transform in Python?
|
View Answer
|
|
How do I apply a method to a sequence of objects?
|
View Answer
|
|
How do I apply a method to a sequence of objects?
|
View Answer
|
|
How do I create a multidimensional list?
|
View Answer
|
|
How do you remove duplicates from a list?
|
View Answer
|
|
How do I iterate over a sequence in reverse order?
|
View Answer
|
|
What's a negative index?
|
View Answer
|
|
How do I convert between tuples and lists?
|
View Answer
|
|
Is there a scanf() or sscanf() equivalent?
|
View Answer
|
|
Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?
|
View Answer
|
|
How do I use strings to call functions/methods?
|
View Answer
|
|
How do I modify a string in place?
|
View Answer
|
|
How do I convert a number to a string?
|
View Answer
|
|
Is there an equivalent of C's "?:" ternary operator?
|
View Answer
|
|
How can my code discover the name of an object?
|
View Answer
|
|
Explain about pickling and unpickling?
|
View Answer
|
|
Explain about repr function?
|
View Answer
|
|
Explain about assert statement?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All Python Interview Questions & Answers - Exam Mode /
Learning Mode
|