-1

I like to make really modular programs but it gets hard to track which functions are subroutines of other functions. Thus, I'd like to define subroutines inside of the parent functions. With Python's object-definition of functions this would be a cogent implementation:

>>> def football():
...     self = football
...
...     logo = "Nike"
...
...     self.display_logo(self)
...
>>> def display_logo(self):
...     print(self.logo)

>>> football.display_logo = display_logo

>>> football()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in football
  File "<stdin>", line 2, in display_logo
AttributeError: 'function' object has no attribute 'logo'

Unfortunately this doesn't work. Neither does it work by trying to access 'logo' by itself. I could define every variable in the function with a self. prefix, but is there any more pragmatic way to create subroutines that have access to the parent function's internal variables upon being called?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Default picture
  • 710
  • 5
  • 12

2 Answers2

0

Try using some actual OOP

class Football:
    def __init__(self):
        self.logo = 'Nike'
        self.display_logo()

    def display_logo(self):
        print(self.logo)

football = Football()
JBernardo
  • 32,262
  • 10
  • 90
  • 115
0

The error is because self, the parameter to display_logo, is not an object with the attributes you think you have. If you try to step through your code, line by line, you'll find a variety of problems. The largest is that you're using your variables as if you have a class defining an object, but you've never defined a class. For the most part, the names you're using are just that: plain names, with no special meanings. As soon as you try to use them in a special way, Python gives you an error message.

You need to follow a simple tutorial on making a class. What you want will look a little more like this:

class Football():
    def __init__(self):
        self.logo = "Nike"
        self.display_logo()

    def display_logo(self):
        print(self.logo)

Football()

Does that help?

Prune
  • 76,765
  • 14
  • 60
  • 81
  • 1
    The error not related to what you wrote. OP simply missed `self` in `self.logo = 'Nike'`. The rest is correct despite being extremely bad OOP. They are using a function object as an object... – JBernardo Apr 19 '17 at 18:35
  • I'm going after the error of design. Yes, you can make the syntax *legal* with that simple change, but the functionality is far from what OP appears to want. – Prune Apr 19 '17 at 18:37
  • Actually it is not. Try doing the change I wrote above and running his code – JBernardo Apr 19 '17 at 18:38
  • I fixed the capital C in class above. – Klaus D. Apr 19 '17 at 18:38
  • I knew how to make classes but I didn't realize I could just put the function code inside of __init__. Thanks :) – Default picture Apr 19 '17 at 18:51
  • You're welcome. note how those functions are indented to be part of the class? That's what makes them class methods, functions that can be invoked only by an object of that class. – Prune Apr 19 '17 at 18:53
  • I know; your answer slightly misses the point, that is, to call Football without assignment. Football() is all that is wanted on the last line. – Default picture Apr 19 '17 at 19:12
  • Ah. In that case, the edit attempt was correct. I'll fix it: you're welcome to instantiate an object and then ignore the created object. – Prune Apr 19 '17 at 19:16