Python
  • index
  • Basic - 1
  • Basic - 2
  • SQLAlchemy
  • Decorator
  • @property
  • __dict__
  • pathlib
  • class
  • flask
  • Jupyter Notebook
  • PyQt
  • UD - FLASK - PY Basic
  • UD - FLASK - REST
  • UD - FLASK - vanilla SQL
  • UD - FLASK - SQLAlchemy
  • UD - FLASK - JWT
  • UD - FLASK - Serialization
Powered by GitBook
On this page
  • 1. @classmethod, @staticmethod
  • 2. Lambda
  • 3. Python folders
  • 4. Array methods

Was this helpful?

Basic - 2

1. @classmethod, @staticmethod

  • @objectmethod: 1st params = self

  • @classmethod: 1st params = cls

  • @staticmethod: no params required

class Store:
    def __init__(self, name):
        self.name = name
        self.items = []

    def add_item(self, name, price):
        self.items.append({
            'name': name,
            'price': price
        })

    def stock_price(self):
        total = 0
        for item in self.items:
            total += item['price']
        return total

    @classmethod
    def franchise(cls, store):
        new_name = '{} - franchise'.format(store.name)
        return cls(new_name)

    @staticmethod
    def details(store):
        return '{}, total stock price: {}'.format(store.name, int(store.stock_price()))


a = Store('Amazon')
a.add_item('macbook', '1100')

Store.franchise(a)
Store.details(a)

2. Lambda

simple

(lambda x: x * 3)(5)

# - - - is equal to - - -

def f(x):
    return x * 3
f(5)

usage

def wrap(another):
    return another()

def add_two_numbers():
    return 35 + 77

wrap(add_two_numbers)

# - - - is equal to - - -
wrap(lambda: 35 + 77)

3. Python folders

folders = package

4. Array methods

map = modify each element, and return array

filter = return array of elements that are true

find= filter & first

reduce = accumulate array, and return a single value

PreviousBasic - 1NextSQLAlchemy

Last updated 5 years ago

Was this helpful?