How can I make use of my class?


#1

I am learning python from minimal knowledge. I’ve had no difficulty until I reached the classes section. And I am highly confused by them.
In the following example:

class Rules():
  def washing_brushes(self):
    return "Point bristles towards the basin while washing your brushes."
  
rule_1 = Rules()
print(rule_1.washing_brushes)

This is what prints:
<bound method Rules.washing_brushes of <main.Rules object at 0x7fb5173814e0>>

I realize that it is showing me the location of this object on the computer, and the object is defined by this class. But how can I make use of the object, or even the class for that matter? Like say… use the variables in the class or define parameters for a variable and pass it through the class to change it and then get it back and actually use it? I’ve searched for a couple of hours for information and am coming up short. Can someone explain how to make use of a class, and even better, give an example?

And this:

class Circle:
  pi = 3.14
  
  def __init__(self, diameter):
    print("New circle with diameter: {diameter}".format(diameter=diameter))
    
  def get_area(self):
    print(((self/2)^2)* Circle.pi)
    
teaching_table = Circle(36)
teaching_table.get_area()

prints: New circle with diameter: 36
Traceback (most recent call last):
File “script.py”, line 12, in
teaching_table.get_area()
File “script.py”, line 9, in get_area
print(((self/2)^2)* Circle.pi)
TypeError: unsupported operand type(s) for /: ‘Circle’ and ‘int’

Isn’t self an “int” at this point (defined by Circle(36)? So why can’t I use it?
Sorry if my questions are ambiguous. I’m getting discouraged.


#2

I think maybe I figured it out…
In part 1:
changing it to:
print(rule_1.washing_brushes())
appears to output what I’m looking for.

and in part 2:
it should be self.pi, but I still can’t use the operands on it…


#3

methods, just like function require parentheses when called, which you do correctly here:

teaching_table.get_area()

yet for some reason, you don’t do it when calling washing_brushes.

no, self refers to the current instance. If you have multiple instances:

teaching_table = Circle(36)
teaching_table_2 = Circle(37)

and you would call get_area on other instances, self is there to help you on which instance the method is called.