Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Object Oriented Programming in Python, Definitions

Defining basic terminologies in python for working on object oriented concepts

Uploaded by

M S S
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Object Oriented Programming in Python, Definitions

Defining basic terminologies in python for working on object oriented concepts

Uploaded by

M S S
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Object Oriented Programming in

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

• In a class can be basic (parent) classes (superclasses),


which (if any) are listed in parentheses after the
class A:
defined
passclass.

• The smallest possible class definition looks like this:


Class Definition
• In the terminology of the Python members of the class are called attributes, functions of the
class - methods and fields of the class - properties (or simply attributes).
• Definitions of methods are similar to the definitions of functions, but (with some exceptions, of
which below) methods always have the first argument, called on the widely accepted
agreement self:

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

def myMethod (self, x):


return x * x

A.m1 = myMethod
A.attr1 = 2 * 2

You might also like