top of page
  • Writer: VC Healy
    VC Healy
  • Jun 16, 2020
  • 1 min read

I liked the idea of being able to iterate through various iterable objects but when I heard about generators I was curious to find out more. As they can be so powerful at controlling the flow of data from an iterable instance to not only slow down this flow but to stop the need to keep regenerating instances of the object.


Of course I am just at the start of my understanding of this and hopefully by the end of the section on Generators in the course I am studying I will have an even better understanding of generators.

 
 
  • Writer: VC Healy
    VC Healy
  • Jun 15, 2020
  • 1 min read

For a long time I never understood why I was placing self into functions.

I did it because it worked but I just never understood why. I knew the function would give me instances and I understood what an instance was.


During my deep diving in Python something sparked that I really need to understand why I was adding in self to a function. It wasn't until I was creating a simple employee function that built a list of employees that it sank in.


Forget all the detailed information of what is required for an employee record and focusing on just one attribute, first_name, the example function is


def employee(self, first_name):

self.first_name = first_name


Adding employees.

employee1 = employee(Jim)

employee2 = employee(John)


'employee1' is the self attribute and 'Jim' the first_name

'employee2' is the self attribute and 'John' the first_name


self is used the attribute for the instance symbol.


 
 
  • Writer: VC Healy
    VC Healy
  • May 27, 2020
  • 1 min read

object.__len__(self)

Called to implement the built-in function len().

Should return the length of the object, an integer >= 0.

Also, an object that doesn’t define a __bool__() method and whose __len__() method returns zero is considered to be false in a Boolean context.

CPython implementation detail: In CPython, the length is required to be at most sys.maxsize. If the length is larger than sys.maxsize some features (such as len()) may raise OverflowError. To prevent raising OverflowError by truth value testing, an object must define a __bool__() method.


__len__(self)

Returns the length of the container. Part of the protocol for both immutable and mutable containers.


len(s)

Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

 
 

© 2020 by Vincent Healy. Proudly created with Wix.com

bottom of page