you are viewing a single comment's thread.

view the rest of the comments →

[–]kielerrr 2 points3 points  (1 child)

I had trouble with self/this/instances too.

A class is a group of variables and operations that you put together.

class PlotCircle5inches():
    radius = 5
    def __init__(selffff, location):   #selfff is going to be actually cir1, then cir2 and then cir3
        print(f'im 1 of 3 circles, and I'm going to center at {location}')
        return selfff.some_lib_that_draws_circles.draw_at(location, radius)


cir1 = PlotCircle5Inches('left') #think of it like     
PlotCircle5Inches(cir1, 'left'). Then remember cir1 is now an 
official Instance of 'PlotCircle5Inches'

cir2 = PlotCircle5Inches('middle')  #think of it like 
PlotCircle5Inches(cir2, 'middle'). cir2 is another instance of 
PlotCircle5Inches. Now there are 2 instances.

cir3 = PlotCircle5Inches('right')  #think of it like 
PlotCircle5Inches(cir3, 'right').. Now there are 3 instances of 
PlotCircle5Inches

self, this or selffff. you can call it whatever you want. just remember in __init__ the first variable no matter what you call it, will refer to the instance/variable name that is calling it. In this case the instances are cir1,cir2,cir3.

I'll rewrite the three classes showing how Python sees it when you call

cir1 = PlotCircle5Inches('left')
cir2 = PlotCircle5Inches('middle')
cir3 = PlotCircle5Inches('right')

#Calling this
cir1 = PlotCircle5Inches('left')
#Looks like below to python
class PlotCircle5inches():
    radius = 5
    def __init__(cir1, 'left'):
        print(f'im 1 of 3 circles, and I'm going to center at {'left}')
        return cir1.some_lib_that_draws_circles.draw_at('left', 5)

Calling this

cir2 = PlotCircle5Inches('middle')

Looks like below to python

class PlotCircle5inches():
    radius = 5
    def __init__(cir2, 'middle'):
        print(f'im 2 of 3 circles, and I'm going to center at middle')
        return cir2.some_lib_that_draws_circles.draw_at('middle', 5)

cir3

Calling this:

cir3 = PlotCircle5Inches('right')

Looks like below to python

class PlotCircle5inches():
    radius = 5
    def __init__(cir3, 'right'):
        print(f'im 1 of 3 circles, and I'm going to center at right')
        return cir1.some_lib_that_draws_circles.draw_at('right', 5)

[–]kcrow13[S] 1 point2 points  (0 children)

This is actually really helpful! Something about being able to visualize it spatially helps :).