Object Oriented Programming in Python, Definitions
Object Oriented Programming in Python, Definitions
Python
Objects, names and
references
• All values are objects
• A variable is a name referencing an object
• An object may have several names referencing it
• Important when modifying objects in-place!
• You may have to make proper copies to get the
effect you want
Fora immutable
• >>> = [1, 3, 2] objects (numbers,
a
strings), this is
[1, 3, 2]
never a problem
>>> b = a
>>> c = b[0:2] b
>>> d = b[:]
c [1, 3]
>>> b.sort() # 'a' is affected!
>>> a
[1, 2, 3]
d [1, 3, 2]
Class Definition
• For clarity, in the following discussion we consider the
definition of class in terms of syntax. To determine the
class, you use class operator:
class ClassName (superclass 1, superclass 2, ...)
# Define attributes and methods of the class
class A:
def m1 (self, x):
# method code block
• Definitions of attributes - the usual assignment operators that connect some of the values with
attribute names:
class A:
attr1 = 2 * 2
Class Definition
• In Python a class is not something static after the definition, so you can add attributes and
after:
class A:
pass
A.m1 = myMethod
A.attr1 = 2 * 2