Calvin (Deutschbein)
W10Fri: 01 Nov
"".join("hi","world") # this works
join("hi","world") # this doesn't
"".len("hi") # this doesn't
len("hi") # this does
xs = [1,2,3]
x = xs.sort()[0] # error
x = 7
class Box:
def __init__(self):
self.x = 7
class Box:
def __init__(self):
self.x = 7
self.make_pos = abs # "absolute value"
b = Box()
print(
b.x,
abs(-2.5),
b.make_pos(-1.5)
)
7 2.5 1.5
b = Box()
print(
b.x, # should be 7
abs(-2.5), # should be 2.5
b.make_pos(-1.5) # should be 1.5
)
7 2.5 1.5
class Box:
def __init__(self):
self.x = 7
self.make_pos = abs # "absolute value"
class Box:
x = 7
make_pos = abs # "absolute value"
class Box:
x = 7
def print_x():
print(self.x)
b = Box()
b.print_x() # this errors
TypeError: Box.print_x() takes 0 positional arguments but 1 was given
abs(-1,-2)
TypeError: abs() takes exactly one argument (2 given)
class Box:
x = 7
def print_x():
print(self.x)
def __init__(self): # what is self?
pass
class Box:
x = 7
def print_x(self): # add self
print(x)
b = Box()
b.print_x() # this works
7
pixels = image.get_pixel_array()
print(pixels[0][0])
print(GImage.get_red(pixels[0][0]))
GImage.get_red(pixels[0][0])
class Box:
# no x here
def print_7(): # back to no self
print(7)
Box.print_7()
TypeError: Box.print_x() takes 0 positional arguments but 1 was given
class Box:
def __init__(self, data):
self.val = data
>>> b = Box(13)
>>> b.val
13
>>> b.val = 17
>>> b.val
17
class Box:
def __init__(self, data):
self._val = data
>>> b = Box(13)
>>> b._val # bad form, but allowed
13
class Box:
def __init__(self, data):
self._val = data
def get_val(self):
return self._val
def set_val(self, data):
self._val = data
class Box:
def __init__(self, data):
self._val = data
def get_val(self):
return self._val
def set_val(self, data):
self._val = data |
>>> b = Box(13)
>>> b._val # bad/wrong
13
>>> b._val = 17 # bad/wrong
>>> b._val # bad/wrong
17
|
class Box:
def __init__(self, data):
self._val = data
def get_val(self):
return self._val
def set_val(self, data):
self._val = data |
>>> b = Box(13)
>>> b.get_val()
13
>>> b.set_val(17)
>>> b.get_val()
17
|
class IntBox:
def __init__(self, data):
if type(data) == int:
self._val = data
else:
self._val = 0
def get_val(self):
return self._val
def set_val(self, data):
if type(data) == int:
self._val = data |
>>> b = IntBox("hi")
>>> b.get_val()
0
>>> b.set_val(7)
>>> b.get_val()
7
>>> b.set_val(print)
>>> b.get_val()
7
|
class IntBox:
def __init__(self, data):
if type(data) == int:
self._val = data
else:
self._val = 0
def get_val(self):
return self._val
def set_val(self, data):
if type(data) == int:
self._val = data |
>>> b = IntBox(7)
>>> b._val *= 3 # bad form
>>> b.get_val()
21
>>> b.set_val(b.get_val() // 3)
>>> b.get_val()
7
|
>>> b
<test.Box object at 0x0000025528D1E9F0>
class Box:
def __init__(self, data):
self.val = data
def __repr__(self):
return "Box(" + str(self.val) + ")"
>>> b = Box(7)
>>> b
Box(7)
>>> b.val **= 2
>>> b
Box(49)
__str__ | Determines what is returned when applying 'str()' to the object |
__abs__ | Determines what is returned when applying 'abs()' to the object |
__and__ | Determines what is returned when object 'and' object is calculated |
__lt__ | Determines what is returned when object '<' object is calculated |
>>> x = 7
>>> type(7)
<class 'int'>
>>> x.__add__(4)
11