nested classes - how to use function from parent class?

2024/11/15 0:31:11

If I have this situation:

class Foo(object):def __init__(self):self.bar = Bar()def do_something(self):print 'doing something'class Bar(object):def __init(self):self.a = 'a'def some_function(self):

I want to call do_something function inside some_function function but this function doesn't belong to the class, what can I do to call this function? I don't want to use it with Foo().do_something, there are another option? I don't want to create new instance

another example:

class A(object):def __init__(self):self.content = 'abcdabcabcabc'self.b = self.B()self.c = self.C()    def some_function(self):print self.contentclass B(object):def foo(self):A.some_function()class C(object):def foo(self):A.some_function()
Answer

There is no practical use case for nested classes in Python, but for scoping some class attributes with namespaces. And in that case, you should not create instances of them at all.

All you get if you have instances of nested classes is a headache - there is no benneffit. The "Outter" class won't see them as anything special - that is unlike in C++, from where it looks like this pattern originated, that the nested class is, in its whole, private to the container class.

The concept of being private in Python is done purely by convention, and if no other code than Foo should use instances of Bar, indicate that by calling it _Bar and in the documentation.

Other than that being nested won't help Bar to get a reference to Foo by any other means than through its name (ok, there are ways using the descriptor protocol, but it is not meant for this) - and them, if you want to run Foo.do_something without having a Foo instance, do_something should be a classmethod anyway.

Now, if you want to have aggregated objects, that is another thing. You have to do is:

class Bar(object):def __init(self, parent):self.parent = parentself.a = 'a'def some_function(self):self.parent.do_something(...)class Foo(object):def __init__(self):self.bar = Bar(self)def do_something(self):print 'doing something'
https://en.xdnf.cn/q/119347.html

Related Q&A

CUDA Function Wont Execute For Loop on Python with Numba

Im trying to run a simple update loop of a simulation on the GPU. Basically there are a bunch of "creatures" represented by circles that in each update loop will move and then there will be a…

Implementing the Ceaser Cipher function through input in Python

Im trying to create a Ceaser Cipher function in Python that shifts letters based off the input you put in.plainText = input("Secret message: ") shift = int(input("Shift: "))def caes…

Twitter scraping of older tweets

I am doing a project in which I needed to get tweets from twitter, and I used the twitter API but it only gives tweets from 7-9 days old but I want a few months older tweets as well. So I decided to sc…

Bootstrap Navbar Logo not found

Hello I am trying to get my NavBar on bootstrap to show a Logo, I have tried moving the png to different folders in the app but I get this error: System check identified no issues (0 silenced). January…

Why camelcase not installed?

i try to install camelcase in my python project. pip install camelcase but when i want to use the package, pylance give me this error: Import "camelcase" could not be resolved Pylance (report…

Find the two longest strings from a list || or the second longest list in PYTHON

Id like to know how i can find the two longest strings from a list(array) of strings or how to find the second longest string from a list. thanks

Which tensorflow-gpu version is compatible with Python 3.7.3

Actually, I am tired of getting "ImportError: DLL load failed" inWindows 10 CUDA Toolkit 10.0 (Sept 2018) Download cuDNN v7.6.0 (May 20, 2019) / v7.6.4 tensorflow-gpu==1.13.1 / 1.13.2 / 1.14 …

Find valid strings [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…

What is the difference in *args, **kwargs vs calling with tuple and dict? [duplicate]

This question already has answers here:What does ** (double star/asterisk) and * (star/asterisk) do for parameters?(28 answers)Closed 8 years ago.This is a basic question. Is there a difference in doi…

Get result from multiprocessing process

I want to know if is there a way to make multiprocessing working in this code. What should I change or if there exist other function in multiprocessing that will allow me to do that operation.You can c…