Posts

Showing posts from June, 2019

More on Python class, attributes and naming.

I visited the learncodethehardway forum, and someone had an excellent question.

There was a bit of an exercise where there was some confusion because of naming and where/what attributes were being set, what code was actually doing the work within the script.

I made these two tiny examples to show the user what was going on in the python class.

I wanted to put it in the blog for anyone else that may have some confusion with what is going on, because I know when I started, I sure did struggle with it.


I'll put comments in the code to explain:

~~~~~~~~~~~ Example 1 ~~~~~~~~~~
--start code block--
class Spam(object): def __init__(self): # this is an attribute. It can be anything-- Even a class self.something = Hello() def spam(self): #self.something is an instance of Hello class self.something.hiYa() class Hello(object): #This is a python object that holds a method def hiYa(self): print("Hello") …