Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
6bdda88
add examples
vtejapy Dec 16, 2017
a1b1eb3
Update examples.md
vtejapy Dec 18, 2017
a9e5c76
Update index.md
vtejapy Dec 18, 2017
f695f92
Update index.md
vtejapy Dec 18, 2017
b043438
Update index.md
vtejapy Dec 18, 2017
db8ab34
Create Introduction.md
vtejapy Dec 19, 2017
b80a7a9
Update Introduction.md
vtejapy Dec 19, 2017
8489d2e
Update Introduction.md
vtejapy Dec 19, 2017
d991c46
Create Chapter-2
vtejapy Dec 19, 2017
97773be
Create chap-2.md
vtejapy Dec 19, 2017
23c273d
Delete Chapter-2
vtejapy Dec 19, 2017
6e459d9
Update chap-2.md
vtejapy Dec 19, 2017
1f7d458
Update index.md
vtejapy Dec 19, 2017
c55b19d
Create chap-3.md
vtejapy Dec 19, 2017
4141393
Update chap-3.md
vtejapy Dec 19, 2017
e3152c0
Update Introduction.md
vtejapy Dec 19, 2017
4065018
Update Introduction.md
vtejapy Dec 19, 2017
c171edf
Update Introduction.md
vtejapy Dec 19, 2017
699f39e
Update Introduction.md
vtejapy Dec 19, 2017
a4bb2aa
Update Introduction.md
vtejapy Dec 19, 2017
89aa005
Create func.md
vtejapy Dec 20, 2017
088c2af
Update func.md
vtejapy Dec 20, 2017
914ad08
Update func.md
vtejapy Dec 20, 2017
de4c9e6
Update func.md
vtejapy Dec 20, 2017
752dc18
Update func.md
vtejapy Dec 20, 2017
9f33ccc
Create Condition.md
vtejapy Dec 20, 2017
6a41663
Update Condition.md
vtejapy Dec 20, 2017
780779f
Update Condition.md
vtejapy Dec 20, 2017
3bc004c
Add files via upload
vtejapy Dec 20, 2017
021296f
Update Condition.md
vtejapy Dec 20, 2017
7843253
Update Condition.md
vtejapy Dec 20, 2017
ae493e9
Update Condition.md
vtejapy Dec 20, 2017
d7e1995
Update Condition.md
vtejapy Dec 20, 2017
e5438e6
Update Condition.md
vtejapy Dec 20, 2017
1cf2f67
Create Iteration.md
vtejapy Dec 20, 2017
afdc158
Update Iteration.md
vtejapy Dec 20, 2017
16ef542
Create Collections.md
vtejapy Dec 20, 2017
abefcff
Update Collections.md
vtejapy Dec 20, 2017
b3f72f1
Update Collections.md
vtejapy Dec 20, 2017
6323bd3
Create Exception_Handling.md
vtejapy Dec 21, 2017
ec47cf9
Create exception-sample-2.md
vtejapy Dec 21, 2017
480c7f8
Create class-sample-1.md
vtejapy Dec 21, 2017
c530f31
Create Class.md
vtejapy Dec 21, 2017
e59c673
Update Class.md
vtejapy Dec 21, 2017
6452e28
Update Class.md
vtejapy Dec 21, 2017
a8619ae
Create File_handling.md
vtejapy Dec 21, 2017
c1423dd
Create regular-expression.md
vtejapy Dec 21, 2017
6f24e25
Create python-database.md
vtejapy Dec 21, 2017
66ed628
Create modulue.md
vtejapy Dec 21, 2017
3c4d6fb
Create README.md
vtejapy Dec 21, 2017
920364c
Update index.md
vtejapy Dec 21, 2017
f553608
Create json.md
vtejapy Dec 22, 2017
fd9f78c
Update json.md
vtejapy Dec 22, 2017
89b141f
Update Introduction.md
vtejapy Dec 22, 2017
3190e5a
Update Introduction.md
vtejapy Dec 22, 2017
3a6c7da
Update Introduction.md
vtejapy Dec 22, 2017
9ace43e
Update Introduction.md
vtejapy Dec 22, 2017
062bffc
Update Introduction.md
vtejapy Dec 22, 2017
dc0664e
Update Introduction.md
vtejapy Dec 22, 2017
3ac98ee
Create Variables.md
vtejapy Jan 3, 2018
a9f75ce
Update Variables.md
vtejapy Jan 3, 2018
0b8dff8
Create Operators_and_expressions.md
vtejapy Jan 3, 2018
2eed46d
Update Variables.md
vtejapy Jan 3, 2018
141bea9
Add files via upload
vtejapy Jan 3, 2018
e915cb1
Create Strings.md
vtejapy Jan 3, 2018
b2d2de4
Update Strings.md
vtejapy Jan 3, 2018
bfd1d1c
Update Strings.md
vtejapy Jan 3, 2018
03b4825
Update Collections.md
vtejapy Jan 4, 2018
9d515b7
Update Collections.md
vtejapy Jan 4, 2018
8ae3dc2
Update README.md
vtejapy Jan 4, 2018
c211133
Update README.md
vtejapy Jan 4, 2018
949df25
Update Operators_and_expressions.md
vtejapy Jan 5, 2018
82373a9
Update Condition.md
vtejapy Jan 5, 2018
78ae850
Update Iteration.md
vtejapy Jan 5, 2018
27e5b1a
Update Exception_Handling.md
vtejapy Jan 5, 2018
7ec2d4e
Update func.md
vtejapy Jan 5, 2018
70d036d
Update func.md
vtejapy Jan 5, 2018
e5c46be
Update func.md
vtejapy Jan 5, 2018
0f7a143
Update index.md
vtejapy Jan 5, 2018
fbb14f5
Update modulue.md
vtejapy Jan 5, 2018
5e0d359
Update modulue.md
vtejapy Jan 5, 2018
8f5015a
Update func.md
vtejapy Feb 7, 2018
86c1192
Update Condition.md
vtejapy Feb 14, 2018
a09ffd2
Create shutil.md
vtejapy Sep 25, 2018
508d721
Create logging.txt
vtejapy Sep 25, 2018
2f4c3ff
Rename shutil.md to shutil.txt
vtejapy Sep 25, 2018
36725e4
Create argparse.txt
vtejapy Sep 27, 2018
40f7f1a
Create config_parser
vtejapy Oct 4, 2018
58ceee0
Create decorators.txt
vtejapy Oct 4, 2018
e4efe82
Create exercise1.md
vtejapy Oct 5, 2018
4e250b3
Update decorators.txt
vtejapy Oct 5, 2018
d4d93fe
Update decorators.txt
vtejapy Oct 5, 2018
a5182ad
Update decorators.txt
vtejapy Oct 5, 2018
3fa15af
Add files via upload
vtejapy Oct 8, 2018
60d0bf7
added mit materials
Oct 25, 2018
6c8012e
Update Condition.md
vtejapy Nov 1, 2018
4c4cb47
added OOP
Nov 5, 2018
f222c88
added oop
Nov 12, 2018
237d300
Update README.md
vtejapy Jul 10, 2019
9c6c570
Update README.md
vtejapy Jul 10, 2019
b412059
Update Introduction.md
vtejapy Feb 18, 2020
9e47db1
Create basics.md
vtejapy Feb 18, 2020
bbe2a43
Update basics.md
vtejapy Feb 18, 2020
4520920
Update basics.md
vtejapy Feb 18, 2020
9ab7fb0
Update README.md
vtejapy Feb 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
279 changes: 279 additions & 0 deletions Class.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
# Classes
Classes model the behavior of objects in the "real" world. Methods implement the behaviors of these types of objects. Member variables hold (current) state. Classes enable us to implement new data types in Python.

The `class:` statement is used to define a class. The `class:` statement creates a class object and binds it to a name.
A simple class

```
class A:
pass

a = A()
```

To define a new style class (recommended), inherit from object or from another class that does. Example:
```
class A(object):
pass


a = A()
a
<__main__.A object at 0x82fbfcc>
```
## Defining methods
A method is a function defined in class scope and with first parameter self:

```
class B(object):
def show(self):
print 'hello from B'

b = B()
b.show()
hello from B
```
A method as we describe it here is more properly called an instance method, in order to distinguish it from class methods and static methods.

## The constructor

The constructor is a method named __init__.

```
class A(object):
def __init__(self, name):
self.name = name
def show(self):
print 'name: "%s"' % self.name

a = A('dave')
a.show()
name: "dave"
```

Notes:

The self variable is explicit. It references the current object, that is the object whose method is currently executing.
The spelling ("self") is optional, but everyone spells it that way.

## Member variables

Defining member variables -- Member variables are created with assignment. Example:

```
class A(object):
def __init__(self, name):
self.name = name
```
A small gotcha -- Do this:
```
class A(object):
def __init__(self, items=None):
if items is None:
self.items = []
else:
self.items = items
```
Do not do this:
```
class B:
def __init__(self, items=[]): # wrong. list ctor evaluated only once.
self.items = items

```

In the second example, the def statement and the list constructor are evaluated only once. Therefore, the item member variable of all instances of class B, will share the same value, which is most likely not what you want.


## Calling methods
* Use the instance and the dot operator.
* Calling a method defined in the same class or a superclass:
* From outside the class -- Use the instance:

```
some_object.some_method()
an_array_of_of_objects[1].another_method()
```
From within the same class -- Use self:

```
self.a_method()
```
From with a subclass when the method is in the superclass and there is a method with the same name in the current class -- Use the class (name) or use super:

```
SomeSuperClass.__init__(self, arg1, arg2) super(CurrentClass,
self).__init__(arg1, arg2)
```
Calling a method defined in a specific superclass -- Use the class (name).

## Class variables

* Also called static data.

* A class variable is shared by instances of the class.

Define at class level with assignment. Example:

```
class A(object):
size = 5
def get_size(self):
return A.size
```
Reference with classname.variable.
```
Caution: self.variable = x creates a new member variable.
```
## Class methods and static methods

### Instance (plain) methods:

An instance method receives the instance as its first argument.

### Class methods:
* A class method receives the class as its first argument.
* Define class methods with built-in function classmethod() or with decorator @classmethod.
* See the description of classmethod() built-in function at "Built-in Functions": http://docs.python.org/2/library/functions.html#classmethod

```
class C(object):
@classmethod
def fun(cls, arg1, arg2, ...):
....
fun: function that needs to be converted into a class method
returns: a class method for function.
```

### Static methods:
* A static method receives neither the instance nor the class as its first argument.
* Define static methods with built-in function staticmethod() or with decorator @staticmethod.
* See the description of staticmethod() built-in function at "Built-in Functions": http://docs.python.org/2/library/functions.html#staticmethod

```
class C(object):
@staticmethod
def fun(arg1, arg2, ...):
...
returns: a static method for function fun.

```


## Inheritance

In order to inherit fro, an existing class, use the following syntax:
```
class Animal(object):
def __init__(self, name="unknown"):
self.name = name
def __str__(self):
print("I'm a {0}".format(self.name))

def Cat(Animal):
def __init__(self):
super().__init__(name='CAT')
```
Here, the parent’s class Animal is initialised inside the child class using the super keyword. The Cat inherits from Animal and therefore has the method print already available.
## Multiple inheritance

if Python cannot find a variable or method in the local namespace, it will perform a depth first search of the super classes in the same order in which the superclasses are specified in the class definition.

That’s all you need to know !! Let us study an example:
```
class Vehicle(object):
def __init__(self, name, color, wheel):
self.name = name
self.wheels = None
self.color = None

def set_wheels(self, n):
self.wheels = 2

def __str__(self):
txt = str(self.name) + ":" + registration
return txt

class TwoWheeler(object):

def __init__(self, name, color):
self.name = name
self.set_wheels(2)
self.color = None

def __str__(self):
return self.wheels

class MotorVehicle(object):

def __init__(self, name, color, power):
self.name = name
self.set_wheels(2)
self.color = None
self.power

class Bicycle(TwoWheeler, gear):

def __init__(self, name, color, gear):
self.name = name
self.set_wheels(2)
self.color = None
```


## property

The property built-in function enables us to write classes in a way that does not require a user of the class to use getters and setters. Example:

```
class Person:
def __init__(self, height):
self.height = height

def get_height(self):
return self.height

def set_height(self, height):
self.height = height

jane = Person(153) # Jane is 153cm tall

jane.height += 1 # Jane grows by a centimetre
jane.set_height(jane.height + 1) # Jane grows again

```

The property built-in function is also a decorator. So, the following is equivalent to the above example:

```
class Person:
def __init__(self, name, surname):
self.name = name
self.surname = surname

@property
def fullname(self):
return "%s %s" % (self.name, self.surname)

@fullname.setter
def fullname(self, value):
# this is much more complicated in real life
name, surname = value.split(" ", 1)
self.name = name
self.surname = surname

@fullname.deleter
def fullname(self):
del self.name
del self.surname

jane = Person("Jane", "Smith")
print(jane.fullname)

jane.fullname = "Jane Doe"
print(jane.fullname)
print(jane.name)
print(jane.surname)

```

Loading