-3

As a title, I have a versatility function in parent class that will share use in child class A.k.A inner class. In below, I need to pass outer_send function from parent class. then, use it with call inner_send function inside Identify class alias child class. The result will output Test.

class Device:

  def __init__(self):
    self.identify = self.Identify(self.outer_send())

  def outer_send(message):
    print(message)

  def last_error(self):
    return self.identify.error_info

  class Identify:
    def __init__(self, send):

     self.inner_send() = send()

    def set_error(self, error):
      self.error_info = error

device = Device()

device.identify.inner_send('test')
David
  • 11
  • 4
  • 2
    Why would you define `Identity` within `Device`? – Brian Dec 01 '19 at 23:52
  • @BrianJoseph I need to access my class like a javascript style. In Identify class will have many information like **manufacturer**, **model**, **imei**. So the use class will like this. `device.identify.manufacturer`, `device.identify.model`, `device.identify.imei` and so on. – David Dec 01 '19 at 23:56
  • @David Can’t `Identity` objects just have an instance of `Device`? – AMC Dec 01 '19 at 23:59
  • @AlexanderCécile I think can't. Cause, theres a handle that i need to pass to Identify class. My real code is create Modem class that consist of info device, send sms, read buffer. I include a link to see what a handle i means. https://stackoverflow.com/questions/59129879/how-to-access-outer-attribute-class-within-inner-class – David Dec 02 '19 at 00:04
  • 2
    I can't quite figure out what identify/identity are doing here at all. A more normal OOP pattern would just be to have these things as attributes of the Device instance - device.model, device.imei, etc. – Simon Notley Dec 02 '19 at 00:05
  • 1
    @David The question you linked is practically the same as this one, no? – AMC Dec 02 '19 at 00:06
  • @SimonN dont confuse with parent name class Device. It can change to everything to parent name. Inside parent class will consist of SMS, Device and so on child class. The sms consist of set atribute need to send message like, `parent.sms.mode = 1`, `parent.sms.sms_center = "+62377328"` and function `parent.sms.send()`. My problem here i have a function in parent that will share use to all child class. So i don't need to create same function in every child. The point is re-use function. So the code more short and clean. – David Dec 02 '19 at 00:12
  • @AlexanderCécile No, the question i mention in link is access outer attribute. But, if u see the question, u will know what the handle i means. I can access outer attribute from inner class. But, at now, i need to share use parent function class to child class. – David Dec 02 '19 at 00:14
  • @David I don’t understand what you’re trying to do. Can you explain it differently, come up with an example, a sketch...? To me this seems like entirely a design issue. – AMC Dec 02 '19 at 00:27
  • How many times have you asked essentially this same question? https://stackoverflow.com/q/59130609/11301900 That’s three different posts! – AMC Dec 02 '19 at 00:40
  • @David Don’t be dishonest with me. It’s essentially the same question, and the answers are the same every time: Don’t do it. It’s poor design. – AMC Dec 02 '19 at 00:45
  • @AlexanderCécile Whatever, what the benefit i doing this thing? But if u dont recognize the different each post. I think u should go take a break and comeback later. – David Dec 02 '19 at 00:48
  • It is the same question, functions are just objects, they are accessed the same way as *any attribute*, because a function can be an attribute – juanpa.arrivillaga Dec 02 '19 at 01:36

1 Answers1

-1

I don't like the pattern and I would recommend designing it differently. However, this does what I think you want to do:

class Device:

  def __init__(self):
      self.identify = self.Identify(self._send)

  def _send(self, message):
      print(message)

  class Identify:
      def __init__(self, _send):
          self.send = _send


device = Device()
device.identify.send('test')

A few notes: I renamed outer_send to _send, as I assume you don't want people calling that directly on the Device object - if you do, just rename it send and it still works; the error bit seemed superfluous, so left it out; your outer_send was missing self as a parameter - it doesn't need it, but if you do want to leave it out, annotate the method with @staticmethod to avoid warnings.

Grismar
  • 27,561
  • 4
  • 31
  • 54
  • Nice, thx. Can u explain what the pattern u said? what the name pattern style. I just familiar with javascript style. – David Dec 02 '19 at 00:41
  • It's not wrong, but you may be better off using something like a SimpleNamespace or a data class, to achieve the same but make it more clear to other programmers (and future you) what you were trying to do. See also here https://www.python.org/dev/peps/pep-0557/ and here https://docs.python.org/3/library/types.html#additional-utility-classes-and-functions – Grismar Dec 02 '19 at 00:43
  • Not sure what the downvote is for, but if people have an issue with the answer, I'd like to know the reason for it - it doesn't look like the question is actually a duplicate of the linked question, even though it appears to be about the same coding problem. – Grismar Dec 02 '19 at 00:49