I have inherited some code which has some very complicated initialization in the constructor that depends on a very complex environment.
I only want to be able to test some functionality therefore just need an empty object, for example one which would have been generated by the default constructor, however the default constructor has been overwritten by some very complex stuff.
I do not have the ability to touch the source code therefore I just need the empty object to be able to call it's functions and test with.
How would I do this? I've looked at mocking but I can't seem to get the actual functionality of the class into the mock object.
UPDATE #1: Example to try to clarify what I'm asking
class Foo(object):
def __init__(self, lots_of_stuff):
lotsofthingsbeingdone()
class Bar(Foo):
def core_functionality(self, something):
val = does_something_important(something)
return val
I want to test core_functionality(). I want to feed it "something" and ensure that the val meets my expectation of what it should be.
Use this wisely. Don't make the legacy mess bigger:
# No constructors executed.
empty_object = object.__new__(YourClass)
I have a situation like below
class MyViewStorage(Object):
# methods
class MyView1(FormView):
# methods
class MyView2(FormView):
# methods
MyViewStorage provides some methods to handle storage in MyView1 and MyView2. These methods require instance attributes created in MyView1 and MyView2.
There are two approaches I have now to use this class.
To inherit MyViewStorage in other two classes something as MyView1(FormView, MyViewStorage). Then process methods by simply using instance attributes created in other two classes.
To create an instance of MyViewStorage by first creating initial instance arguments in MyView1 and MyView2 classes
as shown below
def __init__(self, obj, user, form):
self.obj = obj
self.user = user
self.form = form
Which one is preferred approach?
That Depends on What Kind of Usage you see for MyViewStorage now and in Future.
A. If you know that MyViewStorage will only be used in views like MyView1 and MyView2 and nowhere else. Then it makes a lot of sense that you transfer all common code, present in MyView1 and MyView2 related to storage, in MyViewStorage itself and inherit from it.
PS: You must know how multiple inheritance work in Python, It prioritizes the attributes from left to right in Parent classes (i.e if left most class has the required attribute then it is picked)
B. If you think that MyViewStorage class should be a generic one or can have a lot of extra functionality, which can be used anywhere and can be changed from time to time to make it more generic, then it makes sense to create an instance and use it (because you won't want un-neccessary attributes in your Views which are not useful to you)
Hope this helps
There is some library written on Python (2.x). Its code can't be changed. Inside of the lib there is class A. I need modify behaviour of A::method(). There are a lot of other classes: class B, class C and so on (they all belongs to the lib as well). They all uses class A in some way - no matter. All that means that I can't inherit class A and override the method A::method().
As I know - there is Python-Decorators-technic that should be used for it. But how exactly? Could you show some sample of such solution???
Have you tried:
from lib import A
def my_method(instance, *args, **kwargs):
some_code_here()
A.method = my_method
When I import a module that has a class, what code is executed when that class is first read and the class object created? Is there any way I can influence what happens?
Edit: I realize my question might be a bit too general...
I'm looking for something more low-level which will allow me to do introspection from C++. I extend my C++ application with Python. I have some classes that are defined in C++ and exposed in Python. The user can inherit from these classes in the scripts and I want to be able to grab details about them when they are first defined.
Many possible things can happen. The most basic:
The contents of the class block are executed when the it is first read.
To see this in action, this example:
class Foo(object):
print "bar"
def __init__(self):
print "baz"
Will print bar when the module is imported.
If a class has a metaclass defined, the metaclasses __new__ function will run after the classes block of code is run.
Example:
class MyMeta(type):
def __new__(mcs, name, bases, kwargs):
print "I'm the metaclass, just checking in."
return type.__new__(mcs, name, bases, kwargs)
class Foo(object):
__metaclass__ = MyMeta
print "I'm the Foo class"
Output:
I'm the Foo class
I'm the metaclass, just checking in.
I'm sure other bits can run as well, these are just what I am familiar with.
Defining a class A that inherits from B and C executes: A = type('A', (B, C), m where m is a dictionary containing the members of the class.
You can influence the process using metaclass programming. There are no shortage of blog posts and tutorials on the subject.
You might be interested in metaclasses, which are classes that control the creation of classes.
The code in a class definition is executed just like any other code but any variables created (including function definitions) will be in the context of the class instead of global. This means that you can change the class definition dynamically by adding conditional code:
class A(object):
if 1==2:
def f(self):
print "the world has gone mad"
else:
def f(self):
print "sanity rules"
>>> a = A()
>>> a.f()
sanity rules
>>>
However I have never seen this done, and can't think of a reason for doing it - it feels rather unpythonic.
As others have pointed out there are lots of other ways of modifying the behaviour of a class including metaclasses, inheritance and class decorators.
Python is interpreted, so when a Python module is imported any class code at the module level is run, along with those classes' meta-classes -- this is so the classes will exist.
C++ is compiled: the classes already exist when they are imported; there is no way to control how they are created as they are already created.
I've recently seen several people doing things like this here on Stackoverflow:
class A:
foo = 1
class B:
def blah(self):
pass
In other words, they have nested classes. This works (although people new to Python seem to run into problems because it doesn't behave like they thought it would), but I can't think of any reason to do this in any language at all, and certainly not in Python. Is there such a usecase? Why are people doing this? Searching for this it seems it's reasonably common in C++, is there a good reason there?
The main reason for putting one class in another is to avoid polluting the global namespace with things that are used only inside one class and therefore doesn't belong in the global namespace. This is applicable even to Python, with the global namespace being a namespace of a particular module. For example if you have SomeClass and OtherClass, and both of them need to read something in a specialized way, it is better to have SomeClass.Reader and OtherClass.Reader rather than SomeClassReader and OtherClassReader.
I have never encountered this in C++, though. It can be problematic to control access to the outer class' fields from a nested class. And it is also pretty common to have just one public class in a compilation unit defined in the header file and some utility classes defined in the CPP file (the Qt library is a great example of this). This way they aren't visible to "outsiders" which is good, so it doesn't make much sense to include them in the header. It also helps to increase binary compatibility which is otherwise a pain to maintain. Well, it's a pain anyway, but much less so.
A great example of a language where nested classes are really useful is Java. Nested classes there automatically have a pointer to the instance of the outer class that creates them (unless you declare the inner class as static). This way you don't need to pass "outer" to their constructors and you can address the outer class' fields just by their names.
It allows you to control the access of the nested class- for example, it's often used for implementation detail classes. In C++ it also has advantages in terms of when various things are parsed and what you can access without having to declare first.
I am not a big fan of python, but to me this type of decisions are more semantical than syntactical. If you are implementing a list, the class Node inside List is not a class in itself meant to be used from anywhere, but an implementation detail of the list. At the same time you can have a Node internal class inside Tree, or Graph. Whether the compiler/interpreter allows you to access the class or not is in a different thing. Programing is about writing specifications that the computer can follow and other programers can read, List.Node is more explicit in that Node is internal to List than having ListNode as a first level class.
In some languages, the nested class will have access to variables that are in scope within the outer class. (Similarly with functions, or with class-in-function nesting. Of course, function-in-class nesting just creates a method, which behaves fairly unsurprisingly. ;) )
In more technical terms, we create a closure.
Python lets you do a lot of things with functions (including lambdas) that in C++03 or Java you need a class for (although Java has anonymous inner classes, so a nested class doesn't always look like your example). Listeners, visitors, that kind of thing. A list comprehension is loosely a kind of visitor:
Python:
(foo(x) if x.f == target else bar(x) for x in bazes)
C++:
struct FooBar {
Sommat operator()(const Baz &x) const {
return (x.f == val) ? foo(x) : bar(x);
}
FooBar(int val) : val(val) {}
int val;
};
vector<Sommat> v(bazes.size());
std::transform(bazes.begin(), bazes.end(), v.begin(), FooBar(target));
The question that C++ and Java programmers then ask themselves is, "this little class that I'm writing: should it appear in the same scope as the big class that needs to use it, or should I confine it within the scope of the only class that uses it?"[*]
Since you don't want to publish the thing, or allow anyone else to rely on it, often the answer in these cases is a nested class. In Java, private classes can serve, and in C++ you can restrict classes to a TU, in which case you may no longer care too much what namespace scope the name appears in, so nested classes aren't actually required. It's just a style thing, plus Java provides some syntactic sugar.
As someone else said, another case is iterators in C++. Python can support iteration without an iterator class, but if you're writing a data structure in C++ or Java then you have to put the blighters somewhere. To follow the standard library container interface you'll have a nested typedef for it whether the class is nested or not, so it's fairly natural to think, "nested class".
[*] They also ask themselves, "should I just write a for loop?", but let's suppose a case where the answer to that is no...
In C++ at least, one major common use-case for nested classes is iterators in containers. For example, a hypothetical implementation might look something like this:
class list
{
public:
class iterator
{
// implementation code
};
class const_iterator
{
// implementation code
};
};
Another reason for nested classes in C++ would be private implementation details like node classes for maps, linked lists, etc.
"Nested classes" can mean two different things, which can be split into three different categories by intent. The first one is purely stylistic, the other two are used for practical purposes, and are highly dependent on the features language where they are used.
Nested class definitions for the sake of creating a new namespace and/or organizing your code better. For example, in Java this is accomplished through the use static nested classes, and it is suggested by the official documentation as a way to create more readable and maintainable code, and to logically group classes together. The Zen of Python, however, suggests that you nest code blocks less, thus discouraging this practice.
import this
In Python you'd much more often see the classes grouped in modules.
Putting a class inside another class as part of its interface (or the interface of the instances). First, this interface can be used by the implementation to aid subclassing, for example imagine a nested class HTML.Node which you can override in a subclass of HTML to alter the class used to create new node instances. Second, this interface might be used by the class/instance users, though this is not that useful unless you are in the third case described below.
In Python at least, you don't need to nest the definitions to achieve either of those, however, and it's probably very rare. Instead, you might see Node defined outside of the class and then node_factory = Node in the class definition (or a method dedicated to creating the nodes).
Nesting the namespace of the objects, or creating different contexts for different groups of objects. In Java, non-static nested classes (called inner classes) are bound to an instance of the outer class. This is very useful because it lets you have instances of the inner class that live inside different outer namespaces.
For Python, consider the decimal module. You can create different contexts, and have things like different precisions defined for each context. Each Decimal object can assigned a context on creation. This achieves the same as an inner class would, through a different mechanism. If Python supported inner classes, and Context and Decimal were nested, you'd have context.Decimal('3') instead of Decimal('3', context=context).
You could easily create a metaclass in Python that lets you create nested classes that live inside of an instance, you can even make it produce proper bound and unbound class proxies that support isinstance correctly through the use of __subclasscheck__ and __instancecheck__. However, it won't gain you anything over the other simpler ways to achieve the same (like an additional argument to __init__). It would only limit what you can do with it, and I have found inner classes in Java very confusing every time I had to use them.
In Python, a more useful pattern is declaration of a class inside a function or method. Declaration of a class in the body of another class, as people have noted in other answers, is of little use - yes, it does avoid pollution of the module namespace, but since there_is_ a module namespace at all, a few more names on it do not bother. Even if the extra classes are not intended to be instantiated directly by users of the module, putting then on the module root make their documentation more easily accessible to others.
However, a class inside a function is a completely different creature: It is "declared" and created each time the code containing the class body is run. This gives one the possibility of creating dynamic classes for various uses - in a very simple way. For example, each class created this way is in a different closure, and can have access to different instances of the variables on the containing function.
def call_count(func):
class Counter(object):
def __init__(self):
self.counter = 0
def __repr__(self):
return str(func)
def __call__(self, *args, **kw):
self.counter += 1
return func(*args, **kw)
return Counter()
And using it on the console:
>>> #call_count
... def noop(): pass
...
>>> noop()
>>> noop()
>>> noop.counter
2
>>> noop
<function noop at 0x7fc251b0b578>
So, a simple call_counter decorator could use a static "Counter" class, defined outside the function, and receiving func as a parameter to its constructor - but if you want to tweak other behaviors, like in this example, making repr(func) return the function representation, not the class representation, it is easier to be made this way.
.