minte9
LearnRemember



pag. 270

Definition

A programmer-defined type is called a class.
 
# Class definition
#
# A programmer-defined type is called a class.
# A class definition cannot be empty.

class Point:
    """ a 2D point """ # body docstring comment

class Point:
    def get():
        return (1, 1) # tuple

assert Point.get() ==  (1, 1)
assert Point.get() !=  None

p = Point()

assert Point.get() ==  (1, 1)
assert Point.get() !=  None

Factory

A class is like a factory for creating objects.
 
# A class is like a factory for creating objects.
# Because Point is defined at top level, its name contains __main__

class Point:
    """ a 2D point """

p = Point()

print(p) # __main__.Point object

Dict

Like most Python objects, an empty class has a dictionary by default.
 
# An empty class has a dictionary that ... 
# holds the attributes of the object.

class A(object):
    pass

A = A()
A.__dict__ = {
    'key11': 1,
    'key2': 2,
}

A.__dict__['key2'] = 3
print(A.__dict__['key2']) # 3



  Last update: 87 days ago