Python

Bowen
2 min readMar 23, 2021

This story is a part of — My CS Notebook

Python is a wonderful language, known for its ease of use and ability to tackle complex problems. Part of what makes Python so friendly is, unlike more type-strict languages, memory allocation is limited to the machine the runtime is on, not by the language itself or definition within the code.

These notes are less total language detail and more language overview.

Python as OOP

  • Python can be considered an OOP language
  • Python does not natively support “private variables”, “interfaces”, “final definitions”, “abstractions”, or other common OOP functionalities.
  • Python also does not require that code be written in OOP style.
  • Python does support all types of inheritance, does implement some polymorphic ideas, and does support some level of encapsulation

Encapsulation

  • A Class in Python is an example of encapsulation
  • Python design for private variables is “ — variable name”
class Car():   def __example(self): (private)
pass
def example(self): (non-private)
pass

Scoping & Namespace

  • Python uses namespace like most languages; this prevents name clashing and supports encapsulation
  • A namespace can be thought of as an lego instruction book (it names the pieces, and how to build them)
  • Python uses 4 types of namespaces
§  Built-In- Errors, Exceptions, built-in Methods§  Global- Contains any names defined at the level of the main program
- For example, any modules imported
§ Enclosing / Local- Y is enclosed by X
- when X is called the interpreter creates namespace for both X & Y
- X is the enclosing namespace, Y is the local namespace
  • Python is Lexically Scoped, however, can be overridden by the “global” keyword
Code:name = "Tom" # global variable

def printName():
global name
name = "Jerry"
print("Inside method:", name)
print("Global (Before):", name)
printName()
print("Global (After):", name)
OutputTom
Jerry
Jerry

Immutables

  • Tuples, Strings, and Numbers
  • Immutables have quicker read times but are more expensive to change because they involve a recreation or cloning process
Tuplenames = ("Tom", "Jerry", "Wayne")
names[0] = "Mike" (Throws Error)
Stringname = "supercalifragilisticexpialidocious"
name[5] = "z" (Throws Error)
Numberbefore = 0a = 11
before = id(a)
a += 1
before != id(a)(id() returns id for object), this sequence proves that a new object is created when a string is changed

Mutable Data Types

  • List, Dictionary, Set, and Classes

This story is a part of — My CS Notebook

Enjoy the read?

Leave a clap or comment

Share with friends or on your favorite social platform

--

--