Using variables defined globally vs within classes vs within functions?

Hi. I was working on the Classes projects for the first time, and I was surprised about having to use Class.variable (e.g. Circle.pi in image) to get my variable that was defined within the class, yet I don’t have to do that while within a function with a variable defined within the function. Can someone explain that for me? Thank you.

Screen Shot 2021-02-01 at 10.07.10 PM

A class is an object. That’s the first thing to keep in mind.

>>> class Circle:
	pi = 3.14
	def __init__(self, radius):
		self.radius = radius
	def area(self):
		return self.pi * self.radius ** 2

	
>>> Circle(10).area()
314.0
>>> 

Objects have attributes, many of them inherited from their parent class, over and above those we give when we customize them with our own class definitions.

1 Like
>>> dir(Circle)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'area', 'pi']
>>> 

Scroll to the very end. What do you we find? The very attributes that we defined.

It gets better…

>>> dir(Circle(10))
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'area', 'pi', 'radius']
>>> 

Now scroll all the way to the end. Another attribute has appeared, again of our making.

It should not go unnoticed that we only added three attributes to this object. Python gave us all the rest. They’re dunder methods that should never be used in code. We activate those functions through other means, namely syntax. That is something to learn as we go, and we will learn some of it in this course. Wouldn’t want to spoil that part, but go ahead and look into it as a weekend exploration.


If you want to know how to use Circle,

>>> help(Circle)
Help on class Circle in module __main__:

class Circle(builtins.object)
 |  Circle(radius)
 |  
 |  Help on custom class Circle in <module>:
 |      pi is defined.
 |      The instance will be defined with a radius.
 |      An area method is also defined.
 |  
 |  Methods defined here:
 |  
 |  __init__(self, radius)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  area(self)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  pi = 3.14

>>> 
>>> print (Circle(1).__doc__)

Help on custom class Circle in <module>:
    pi is defined.
    The instance will be defined with a radius.
    An area method is also defined.
    
>>> 
1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.