Software Development - Deepstash

πŸ’»

Software Development

18 STASHED IDEAS

slice

slice(stop)

slice(start, stop, [step])

  • Standard indexing relies on slice():Β a[start:stop:step]Β callsΒ a.__getitem__(slice(start, stop, step)).
  • slice() creates a shallow copy of the selected elements from the original list.
  • Whenever you slice a list, the Python interpreter creates a new list and populates it with references to the elements you selected from the original list. Given that a reference needs 8 bytes on 64-bit systems,Β the overhead of copying references becomes noticeable when working with large lists. For example, slicing 1000 elements allocates almost 8kb worth of references.

STASHED IN:

2

🐍 Python docs & Stack Overflow

STASHED IN:

0 Comments

Returns an infinite iterator that produces evenly spaced values starting with number start.

It's often used in combination with map() to generate consecutive data points, and with zip() to add sequence numbers.

STASHED IN:

2

🐍 Exploring itertools docs

STASHED IN:

0 Comments

How generator functions work

A generator function is any function that has the yield keyword in its body. When called, generator functions return a generator object that wraps the body of the function.

Generator objects can be consumed like any other iterator. When we call next() on them, execution advances to the next yield statement in the function body, and the yielded value is returned. Finally, when the function body returns, the generator object returns StopIteration.

See example in image: gen_AB() returns a generator that can be iterated over. next() is called on it 3 times in total.

STASHED IN:

2

πŸ“– Fluent Python

STASHED IN:

0 Comments

β€Œ
β€Œ
β€Œ
β€Œ
β€Œ
β€Œ
β€Œ
β€Œ
β€Œ
β€Œ
β€Œ
β€Œ
β€Œ
β€Œ
β€Œ
β€Œ
β€Œ
β€Œ

❀️ Brainstash Inc.