Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to nest classes? [closed]

Tags:

python

oop

class

I managed to define a class A, use a list of instance of another class B as class A's instance variable. class B has a function to change class A's other instance variable a1. Class A also has a function to change class B's instance variable bb. So class A can access class B and class B can access class A.

The two classed are nested together. I know we can make things easier to change all instance variables and functions of class B to class A. But in my project, this nested structure is the real way things are.

class A:   class B:       count = 0       def __init__(self,b1=None,b2=None):           self.b1 = b1           self.b2 = b2       def funcb(self,bb):           A.a1 = pyfunc1(bb)   def __init__(self,a1,a2):       self.a1 = a1       self.a2 = a2       self.listb = [A.B()]   def funca(self,aa):       A.B.count += 1       b = A.B(self.a1,self.a2)       listb.append(b)       listb[A.B.count].b1 = listb[A.B.count-1].b1.pyfunc2(aa)       listb[A.B.count].b2 = pyfunc3(aa,self.a2)       listb[A.B.count].funcb(self.a2) 

What I want to know is that if such kind of nested class will reduce the python efficiency? Any better solution?

like image 912
xibinke Avatar asked May 21 '15 14:05

xibinke


People also ask

Is it good to have nested classes?

There are several reasons for using nested classes, among them: It is a way of logically grouping classes that are only used in one place. It increases encapsulation. Nested classes can lead to more readable and maintainable code.

When should you nest classes?

Nested class: Use it if your requirements are similar to those of a local class, you want to make the type more widely available, and you don't require access to local variables or method parameters.

Are nested classes good practice Python?

Though inner or nested classes are not used widely in Python it will be a better feature to implement code because it is straightforward to organize when we use inner class or nested class.

What are disadvantages of using inner classes?

Inner classes have their disadvantages. From a maintenance point of view, inexperienced Java developers may find the inner class difficult to understand. The use of inner classes will also increase the total number of classes in your code.

When should I use a nested class?

Use a nested class when the class you are nesting is only useful to the enclosing class. For instance, nested classes allow you to write something like (simplified): public class SortedMap { private class TreeNode { TreeNode left; TreeNode right; } }

What is the difference between public and private nested classes?

I rarely would ever use a Public nested class but use Private nested classes all of the time. A private nested class can be used for a sub-object that is intended to be used only inside the parent. An example of this would be if a HashTable class contains a private Entry object to store data internally only.

What exceptions do you like to nest?

I like to nest exceptions that are unique to a single class, ie. ones that are never thrown from any other place. For example:

Should I nest functions or write as siblings?

the inside functions may not be a pure function, if they rely on closure variables. If you don't need a closure or don't need to worry about polluting your namespace, write it as a sibling. If you need a closure or don't want to pollute your namespace, nest it.


1 Answers

Nesting a class doesn't reduce nor increase execution efficiency. It may alter maintenance and understanding efficiency.

The nested class becomes just another attribute on the parent class. You'll have to reference it as A.B rather than B. That's it, you deferred the lookup to a different namespace. In other words, your __init__ method will fail, because there is no global name B, only A.B and self.B exist (both referencing the same class object).

There is otherwise no special relationship between a nested class and their parent, as there is in Java.

Most Python developers do not nest classes, so when you do so you break convention and increase maintenance cost.

like image 138
Martijn Pieters Avatar answered Sep 19 '22 19:09

Martijn Pieters