>>> int
<class 'int'>
All variables and expressions in Python are references (semantically equivalent to pointers to objects). If you are familiar with a C++, it is like the following:
object * a = new object(20000);
object * b = a;
a == b // true
A bytes object is an immutable array. The items are 8-bit bytes, represented by integers in the range 0 <= x < 256. Bytes literals (like b'abc') and the built-in bytes() constructor can be used to create bytes objects. Also, bytes objects can be decoded to strings via the decode() method.,A bytearray object is a mutable array. They are created by the built-in bytearray() constructor. Aside from being mutable (and hence unhashable), byte arrays otherwise provide the same interface and functionality as immutable bytes objects.,Called by bytes to compute a byte-string representation of an object. This should return a bytes object.,These represent a mutable set. They are created by the built-in set() constructor and can be modified afterwards by several methods, such as add().
def __hash__(self):
return hash((self.name, self.nick, self.color))
import sys
from types
import ModuleType
class VerboseModule(ModuleType):
def __repr__(self):
return f 'Verbose {self.__name__}'
def __setattr__(self, attr, value):
print(f 'Setting {attr}...')
super().__setattr__(attr, value)
sys.modules[__name__].__class__ = VerboseModule
class Philosopher:
def __init_subclass__(cls, /, default_name, **kwargs):
super().__init_subclass__( ** kwargs) cls.default_name = default_name
class AustralianPhilosopher(Philosopher, default_name = "Bruce"):
pass
class A:
x = C() # Automatically calls: x.__set_name__(A, 'x')
class A:
pass
c = C()
A.x = c # The hook is not called
c.__set_name__(A, 'x') # Manually invoke the hook
class Meta(type):
pass
class MyClass(metaclass = Meta):
pass
class MySubclass(MyClass):
pass
When we talk about objects, there is a similar ambiguity. For example, if two Points are the same, does that mean they contain the same data (coordinates) or that they are actually the same object?,Now if we create two different objects that contain the same data, we can use same_point to find out if they represent points with the same coordinates.,Even though p1 and p2 contain the same coordinates, they are not the same object. If we assign p1 to p3, then the two variables are aliases of the same object:,We’ve already seen the is operator in the chapter on lists, where we talked about aliases: it allows us to find out if two references refer to the same object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class Rectangle:
""
" A class to manufacture rectangle objects "
""
def __init__(self, posn, w, h):
""
" Initialize rectangle at posn, with width w, height h "
""
self.corner = posn
self.width = w
self.height = h
def __str__(self):
return "({0}, {1}, {2})"
.format(self.corner, self.width, self.height)
box = Rectangle(Point(0, 0), 100, 200)
bomb = Rectangle(Point(100, 80), 5, 10) # In my video game
print("box: ", box)
print("bomb: ", bomb)
box: ((0, 0), 100, 200) bomb: ((100, 80), 5, 10)
box.width += 50 box.height += 100
1 2 3 4 5 6 7 8 9 10 11 12
class Rectangle:
#...
def grow(self, delta_width, delta_height):
""
" Grow (or shrink) this object by the deltas "
""
self.width += delta_width
self.height += delta_height
def move(self, dx, dy):
""
" Move this object by the deltas "
""
self.corner.x += dx
self.corner.y += dy