0

Okay, I know this is very confusing to type out. Let me clarify that I feel like this should work. I'll provide a simple example and the error (generalized to the example) that I receive.

class A:
  FLAG = otherclass('flag')

  def __init__(self):
    self.half_val = 0
    self.second_half_val = 0

  def convert_value(self):
    return "{}.{}".format(self.half_val, self.second_half_val)

  def bring_in_B(self):
    temp_val = self.convert_value()
    self.member = B(self, temp_val)
    return self.member

  def __publish(self):
    (do publishing stuff)

class B:
  def __init__(self, A, address):
    self.A = A
    A.cap = cv2.VideoCapture(address)

    self.cap = A.cap

    ...

  def update(self):
    ...
    self.grabbed, self.frame = self.cap.read()
    self.A.__publish(arg1=self.A.FLAG, arg2=self.frame)

Error I receive:

in update
    self.A.__publish(arg1=self.A.FLAG, arg2=self.frame)
AttributeError: 'A' object has no attribute '_B__publish'

I've tried to balance being general enough here while also sharing enough context, but please let me know if more information is needed. This isn't an IP-controlled project, so I can post the true code if necessary (it's just very long and I thought a slightly simpler example might serve better). Obviously I've removed elements where the "..." appears--there are otherwise no syntax issues, just the error I mentioned. The specific functions mentioned (particularly in class A) are also just placeholders here to highlight general functionality, and the real functions are a bit different (but not necessarily more complex to the point of adding any additional considerations).

I cannot, for the life of me, figure out why this isn't working properly, especially since the A.cap declaration works fine to create a new member while inside of another class. I feel like I'm definitely missing something obvious, and my lack of any formal training in Python might be to blame (all learned as needed). Any help or insight is greatly appreciated, cheers!

EDIT: that was quick...and dumb. I fixed it. Can't have it be "__publish" if I want to call it from another class, it has to be "publish". Looking into why. If anyone sees this and wants to participate in sharing why, please be my guest. Otherwise I'll leave this as a testament to other newbs to watch out for this kind of stuff.

  • 1
    Because you used two-leading underscores, `__publish`... why did you do that? The *whole point* of that is to mangle the names, to prevent accidental name collisions in subclasses. You can either use `self.A._A__publish(...)` or simply **don't use double-underscore name-mangling* and jsut naem the function `publish` since apparently `publish` is meant to be called externally. – juanpa.arrivillaga Apr 22 '22 at 06:16
  • @juanpa.arrivillaga, yeah I just didn't know that. I was mixing elements from two different drivers and modifying each to fit my needs, and I've never come across that sort of thing outside of __init__, etc. That whole "self-taught" element.... I understand that now. – Sparky Parky Apr 22 '22 at 06:18
  • Note, " Can't have it be "__publish" if I want to call it from another class, it has to be "publish"." That isn't true, you just have to understand that if you define an instance variable in a class, `MyClass`, e.g. `self.__var = foo`, then the *actual* name stored in the namespace is `_MyClass__var`, so you *could* use `_MyClass__var` outside of the class to access it. – juanpa.arrivillaga Apr 22 '22 at 06:20
  • See here for more details: https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-single-and-double-underscore-before-an-object-name You can also do a websearch on "double underscore python". – Acccumulation Apr 22 '22 at 06:21
  • Here is a [link to the relevant docs](https://docs.python.org/3/tutorial/classes.html#private-variables) – juanpa.arrivillaga Apr 22 '22 at 06:23
  • @juanpa.arrivillaga, that makes sense. I should've noticed the tip in the error, with "_B__publish". I definitely see how I can do that now, and this is a big lesson learned! Useful for the future. EDIT: Thanks for the link! I think between you both I won't have much Google'ing to do at all tomorrow! – Sparky Parky Apr 22 '22 at 06:25
  • @Acccumulation, thank you for the resources! I won't be digging much tonight, but figured after the change I had some research to do. Never even occurred to me until a whim that that might be the issue.... Starting to understand how little I actually understand. Getting better every day! – Sparky Parky Apr 22 '22 at 06:26

0 Answers0