This question already has answers here:
What is the difference between a function, an unbound method and a bound method?
(6 answers)
Closed 2 years ago.
I am switching from MATLAB to Python and numpy and I would like to know if there is any difference between the option to define a class method and the option to the function to a class field (instance variable)? Here is the example:
class MyClass:
def __init__(self, a):
self.a=a #some variable
def add(self,b):
return self.a+b
vs
class MyClass:
def __init__(self, a):
self.a=a #some variable
self.add = lambda b: self.a+b
It works in both cases when I call
my_object=MyClass(2)
print(my_object.add(2)) #prints 4
Are there any differences between these two approaches? Any best practices/downsides?
To me, the first one feels more "proper OOP", but the second one feels more flexible. Or, maybe, the definitions are identical, because of the way Python works under the hood?
The second one can't be overridden and takes a lot more space, because there's a separate function in every instance's __dict__ instead of one function in the class __dict__. (Instance method objects are created and reclaimed on the fly if you do it the normal way, or optimized out entirely in many cases depending on Python version.)
Related
This question already has answers here:
Why won't dynamically adding a `__call__` method to an instance work?
(2 answers)
Closed 4 years ago.
I have a wrapper class similar to this (strongly simplified) example:
class wrap(object):
def __init__(self):
self._data = range(10)
def __getitem__(self, key):
return self._data.__getitem__(key)
I can use it like this:
w = wrap()
print w[2] # yields "2"
I thought I could optimize and get rid of one function call by changing to this:
class wrap(object):
def __init__(self):
self._data = range(10)
self.__getitem__ = self._data.__getitem__
However, I receive a
TypeError: 'wrap' object does not support indexing
for the print w[2] line with the latter version.
The direct call to the method, i.e., print w.__getitem__(2), works in both cases...
Why does the assignment version not allow indexing?
EDIT: Regarding the "closed for duplication"
I agree that the linked question has the same answer. It is, however, not at all clear that they are the same question. In particular, someone who does not know the answer here, also does not know that there is an overarching "type of problem" at work. Thus, it won't be clear that they find the answer in a seemingly unrelated question about __call__.
Special methods (essentially anything with two underscores on each end) have to be defined on the class. The internal lookup procedure for special methods completely skips the instance dict. Among other things, this is so if you do
class Foo(object):
def __repr__(self):
return 'Foo()'
the __repr__ method you defined is only used for instances of Foo, and not for repr(Foo).
This question already has answers here:
Python super() arguments: why not super(obj)?
(2 answers)
Closed 4 years ago.
I have code snipped like :
class A:
def test(self):
return 'A'
class B(A):
def test(self):
return 'B->' + super(B, self).test()
print(B().test())
Output : B->A
If I write something like this then I'm getting the same output :
class A:
def test(self):
return 'A'
class B(A):
def test(self):
return 'B->' + super().test() # change to super()
print(B().test())
In both cases I'm getting the same output. Then, I want to know what's the difference between this two types of calling of super? What are the pros and cons of using either of them?
In Python 2, only the super(className,self) syntax was possible. Since It was the most used version, as of Python 3 providing no arguments will act the same.
There are two typical use cases for super. In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable
This question already has answers here:
Overriding a static method in python
(3 answers)
Closed 6 years ago.
Suppose we declare a static method with same name in different classes. Is it possible?
If it is, then how and which function will be called?
class abc:
#staticmethod
def xyz():
print 'class_abc'
class abc1:
#staticmethod
def xyz():
print 'class_abc1'
class abc2:
#staticmethod
def xyz():
print 'class_abc2'
So what's the output and how we can call different functions of different classes?
You are having three classes with xyz() function in each class. But there is no relationship in these classes as they are not inheriting each other. So the answer is simple: xyz() will be called of the class which is calling the method.
For example: abc.xyz() will call the xyz() function of abc class. Similarly you can make call to ab1 and abc2's function as: abc1.xyz() and abc2.xyz().
You seem to be misunderstanding things. Classes introduce a separate namespace for themselves so, it is completely possible to create functions with the same name in different classes. These functions are not related in any other way other than their similar name.
Running <classname>.xyz() simply calls xyz() and prints the corresponding message.
Even if there was a relationship between the class, i.e a sub-classing relationship of the form:
class abc:
#staticmethod
def xyz():
print 'class_abc'
class abc1(abc):
#staticmethod
def xyz():
print 'class_abc1'
class abc2(abc1):
#staticmethod
def xyz():
print 'class_abc2'
The most recent definition of xyz will override previously existing entries for it and the effect would be the same, that is abc2.xyz() would print class_abc2, abc1.xyz() prints class_abc1 and so on.
Also, do note, you're using Python 2.x but aren't actually inheriting from object. This won't create a class in the sense most people are aware with today, take a look at What is the difference between old style and new style classes in Python? to get a better idea of what this entails.
Suppose we declare a static method with same name in different is it possible?
Yes
If it is possible then how and which function will be called.?
It's possible because classes have their own scope. Python treats each static method in your example differently. And even if your classes where related, such as in #Jim Fasarakis-Hilliard example, the current method would override the last method.
Each method will be called uniquely because each class is unrelated in your example(except for all of the classes being of type class).
This question already has answers here:
Python - calling ancestor methods when multiple inheritance is involved
(2 answers)
Closed 8 years ago.
Consider:
class X:
def some_method(self):
print("X.some_method called")
class Y:
def some_method(self):
print("Y.some_method called")
class Foo(X,Y):
def some_method(self):
super().some_method()
# plus some Foo-specific work to be done here
foo_instance = Foo()
foo_instance.some_method()
Output:
X.some_method called
Switching the class declaration of Foo to instead be:
class Foo(Y,X):
Alters the output to:
Y.some_method called
If I want both ancestor methods to be called I could alter Foo's implementation as:
def some_method(self):
X().some_method()
Y().some_method()
# plus some Foo-specific work to be done here
This leads to my question. Is there any uber secret way to cause Python to invoke the method on all ancestors without me doing so explicitly like the code, such as (I'm making up the all_ancestors keyword here - does such a thing actually exist?):
def some_method(self):
all_ancestors().some_method()
# plus some Foo-specific work to be done here
with an expected output of:
X.some_method called
Y.some_method called
No, there is no secret way to do that. As I mentioned in your other question, the usual way to do this is not to call all ancestor methods from the single descendant class. Instead, each class should use super to call just one ancestor method, namely the next one up the inheritance chain. If every class in the tree does this (except the topmost base class), then all methods will get called in order. In other words, Foo should use super(), which will call X's method; and then X should also use super(), which will call Y's method.
To make this work right, it is usually best to have a single topmost class in the inheritance tree. In your example this would be a class that is the base of both X and Y. You need such a class to serve as a final stop to the sequence of super calling; this base class should not call super. If you just keep calling super everywhere, eventually it will try to call up to the base object class, and then fail because object doesn't provide the method you're trying to call.
If you can provide X & Y with a common base class or mix-in, this should work:
class ISomeMethod:
def some_method(self):
pass
class X(ISomeMethod):
def some_method(self):
print("X.some_method called")
super(X, self).some_method()
class Y(ISomeMethod):
def some_method(self):
print("Y.some_method called")
super(Y, self).some_method()
some_method should then be called in the order which you declare the base classes in Foo.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between #staticmethod and #classmethod in Python?
I am learning OOP in python and came to know about these two methods
It seems that the difference in terms of syntax is that class methods are implicitly passed the class they belong to as their first parameter
class Circle:
all_circles = [] # class variable
#staticmethod
def total_area():
for c in Circle.all_circles: # hardcode class name
# do somethig
#classmethod
def total_area(cls):
for c in cls.all_circles: # no hardcode class name
# do something
I see class method as more flexible since we don't hardcode the class
Question:
- Is it even a question which one is better? #staticmethod or #classmethod?
- what are the scenarios suitable to use of each one of these methods?
A classmethod gets passed the class 'cls' that it was called upon. For more details see: What is the difference between #staticmethod and #classmethod in Python?