Python Book
πŸ‡ΊπŸ‡¦ Stand with UkraineπŸŽ“Training Suite
  • Book overview
  • Notes about this book
  • 1. Introduction to Python
    • What is Python
    • Basic syntax
    • Objects in Python
    • Python overview
    • Installation, IDEs etc.
    • ipython
    • Sources for self-learning
  • 2. Strings and numbers
    • Getting help
    • Introspection
    • Basic types
    • None object
    • Numbers
    • Strings
    • Unicode
    • String Formatting
    • Regular expressions
    • Sources for self-learning
  • 3. Containers
    • Data Structures
    • Lists
    • Tuples
    • Dictionaries
    • Sets
    • Conditions
    • Loops
    • Additional modules
    • Sources for self-learning
  • 4. Functions
    • Functions
    • Scopes of visibility
    • Generators
    • Lambdas
    • Type hints
    • Function internals
    • Sources for self-learning
  • 5. Functional Programming
    • Builtins
    • Iterable
    • Iterator
    • Functional Programming
    • Functools
    • Comprehensions
    • Additional modules
    • Sources for self-learning
  • 6. Code Styling
    • Zen of Python
    • Lint
    • PEP 8
    • Modules
    • Packages
    • Sources for self-learning
  • 7. OOP
    • OOP Basics
    • Code design principles
    • Classes
    • Method Resolution Order
    • Magic attributes and methods
    • Super
    • Sources for self-learning
  • 8. Decorators, Exceptions
    • Decorators
    • Exceptions
    • Sources for self-learning
  • 9. Testing
    • Basic Terminology
    • Testing theory
    • Dev unit testing vs QA automated testing
    • Best Practices
    • Doctest
    • Unittest
    • Test Runners
    • Pytest
    • Nose
    • Continuous Integration
  • 10. System Libs
    • Working with files
    • System libraries
    • Subprocess
    • Additional CLI libraries
Powered by GitBook
On this page
  • Most important builtins
  • Access to object attributes

Was this helpful?

Edit on Git
  1. 5. Functional Programming

Builtins

There are lot of builtin functions (no need to import them) that are always available.

https://docs.python.org/3/library/functions.html

Some example that we already saw:

  • print()

  • len()

  • pow()

  • int()

  • str()

  • type()

  • isinstance()

  • range()

  • sum(), max(), min(), enumerate(), round()...

But there are much more of them!

Built-in Functions

abs()

dict()

help()

min()

setattr()

all()

dir()

hex()

next()

slice()

any()

divmod()

id()

object()

sorted()

ascii()

enumerate()

input()

oct()

staticmethod()

bin()

eval()

int()

open()

str()

bool()

exec()

isinstance()

ord()

sum()

bytearray()

filter()

issubclass()

pow()

super()

bytes()

float()

iter()

print()

tuple()

callable()

format()

len()

property()

type()

chr()

frozenset()

list()

range()

vars()

classmethod()

getattr()

locals()

repr()

zip()

compile()

globals()

map()

reversed()

__import__()

complex()

hasattr()

max()

round()

delattr()

hash()

memoryview()

set()

Most important builtins

  • Types are callable:

    • int()

    • float()

    • str()

    • list()

    • dict()

    • set()

    • bool()

    • frozenset()

    • bytes()

    • bytearray()

  • Introspection:

    • id()

    • dir()

    • type()

    • isinstance()

    • issubclass()

    • callable()

    • hash()

    • help()

  • __import__()

  • len(), print()

  • abs(), sum(), max(), min(), pow(), round()

  • range(), sorted(), reversed(), enumerate(), all(), any()

  • open()

  • globals(), locals(), (not builtins but don't forget about: global, nonlocal)

Access to object attributes

  • getattr(obj, "attr_name"[, default])

  • hasattr(obj, "attr_name")

  • setattr(obj, "attr_name", value)

  • delattr(obj, "attr_name")

πŸͺ„ Code:

class A(object):
    a = 5
    
a_object = A()
print( hasattr(a_object, "a") )
print( getattr(a_object, "a") )
setattr(a_object, "b", "Wow! Adding attrs like haxxxors")
print( a_object.b )

πŸ“Ÿ Output:

True
5
Wow! Adding attrs like haxxxors

πŸͺ„ Code:

getattr("Hello World", "lower")()

πŸ“Ÿ Output:

'hello world'

# Iterable and Iterator

Iterable is the source of data for iterator, usually - some sequence.

[1, 2, 3]
("a", "b")
"Hello!"
{1: 2, "a": 3}
{1, 2, 3}
range(10)
reversed("abc")

Iterator - an abstract object that is capable of yielding "next" item and raising StopIteration in the end.

iter("abce")

next(iter("abce"))
iter("abce").__next__()

for i in "abcd":
    print(i)
Previous5. Functional ProgrammingNextIterable

Last updated 2 years ago

Was this helpful?