Undefined name?

I am learning Python and I like to try any code snippets I come across. This snippet looks ok to me but throws up an undefined name error.

class subject:
    def __init__(self):
        print('Subject is created')
    
    def __del__(self):
        print('Destructor is called')
    
    def object_creation():
        print('\n')
        print('Creation of object1')
        object1 = subject()
        print('End of object_creation')
        return object1
    
print('Call object_creation() function')
object1 = object_creation()
print('End of program>>>>>>>>>>>>>>>>>')

The "object_creation()" is defined. I can not see where the error lies.

You can only call an instance method as an attribute of the class or an instance.
Bug object_creation is a static method and need an @staticmethod decorator. Not for beginners, and static methods are never needed. And there is no reason to create instance within a method. Just call the class.

sub = subject()  # Call subject.__new__ and then subject.__init__.

I suggest you read some of the python code in the stdlib.

Thank you for your response. I got the code sample from a book that is supposed to be teaching beginners. I guess it as a mistake to put it in there, also, the writer could not have tested the code first.

Regards

If the definition of object_creation were not indented, it would not be an instance method of the class subject. It would just be a top-level function, and would not raise the more advanced topics @tjreedy mentions.

I think def object_creation is just indented when it may not be so in the book. Possibly your IDE has “helped” you to this.

2 Likes

Of course. Unindent and the code works and makes sense.

Code in books - especially physical, printed ones, rather than printer-friendly webpages - is often full of bugs. It’s not as easy to revise them, and they get reviewed and edited by people who specialize in writing English rather than code.

While it can be useful, as a way to “internalize” code concepts, to type out an example (not copy-and-paste) carefully and exactly by hand, it won’t always get you very far. It also still requires paying attention to detail. It’s easy to think “oh, the work of figuring out what the code should say has already been done, so I only have to type”; but there are many details that may seem trivial but are actually very important.

As well, many code editors will try to help you by automatically adding matching brackets, suggesting names etc. Even very basic ones will at least attempt to indent lines of code automatically. These features can be useful - but they can also result in typing something you didn’t intend, if you aren’t paying attention and expecting the features to work the way they do.

It’s important to understand the code in detail as you are working through examples. Instead of tutorials that simply walk through the specific process of making some specific project, try to find guides that explain concepts and encourage you to experiment with parts of the code (in controlled ways). Either way, don’t skip any testing steps. After each small change to the code, you should feel like you confidently know:

  • How can we properly check that this code does what it is supposed to do?
  • What should happen differently from the last version of the code, now that the change is made?
  • Does it work? (i.e., actually do the test that you proposed, and see what happens).

Thank you for your insights. I am using Spyder for coding and, as you pointed out, it does most of the syntax for you. I used to code in C using Borlands C. I have not code for over 20 years now. Now I’m 79, and as I spent most of my working life problem solving, I need something to keep my brain alive, hence my return to coding. I guess I fell into the trap of thinking it would be a matter of picking up where I left off.

I have found that Cisco have beginner and intermediate courses in Python, and I have started from the beginning. It’s a little tedious relearning about literal, variable and print(), which are much the same as C, but I do take your point that a proper course is the best way to learn Python, (right down to the guts), properly. I was given this advice when I was young: “The difference between an Austin and a Rolls Royce is a little extra care” Wise words.

Thank you and regards John