Tag: Objects & classes

  • Python is vs ==: Object identity and value equality

    Consider this Python: Surprised? That’s because “is” is the wrong comparison: • == asks: are these the same value?• is asks: are these the same object? You almost never care about question #2. And so, should rarely use “is”.

    Read more

  • The “why” of object-oriented programming (in Python)

    When I ask people — in my corporate training, or when they join my “Better developers” newsletter — what frustrations they have with Python, many people say that it’s object-oriented programming. Even if they have a decent background in procedural (i.e., traditional, non-OO) development, objects confuse and surprise them. That’s a shame, because the whole…

    Read more

  • Let’s de-confuse Python objects!

    Are you confused about object-oriented Python? You’re not alone. In teaching Python to companies around the world for more than 20 years, I’ve found that almost everyone is confused by Python objects: Newcomers to object-oriented programming are just plain ol’ confused — by the terminology, the syntax, and why we even need to use objects.…

    Read more

  • LEGB? Meet ICPO, Python’s search strategy for attributes

    When it comes to variables, Python has a well-known search strategy, known by the acronym “LEGB.” Whenever you mention a variable — and by “variable,” I mean a name that could be referencing data, a function, or a class — Python tries to find it in four different places: The local (function) scope, the enclosing…

    Read more

  • Master Python objects (and understand how they work)

    I’ve been teaching Python for many years now, and it never ceases to amaze me to discover just how many people are able to use Python without ever having really learned it. In particular, there are lots of people writing and using Python objects who assume that classes and instances in Python work just like…

    Read more

  • A short Python class puzzle

    Here’s a short Python puzzle that I use in many of my on-site courses, which I have found to be useful and instructive: Given the following short snippet of Python, which letters will be printed, and in which order? print(“A”) class Person(object):     print(“B”)     def __init__(self, name):         print(“C”)         self.name = name    …

    Read more

  • Python’s objects and classes — a visual guide

    Python developers love to say that “everything is an object.” And indeed, when I teach Python classes, I say this several times, and many people nod in agreement, assuming that I’m merely repeating something they’ve heard before.  After all, people often say that everything in Java is an object (except for the things that aren’t),…

    Read more

  • In Python, it’s all about the attributes

    Newcomers to Python are often amazed to discover how easily we can create new classes. For example: class Foo(object): pass is a perfectly valid (if boring) class. We can even create instances of this class: f = Foo() This is a perfectly valid instance of Foo. Indeed, if we ask it to identify itself, we’ll find…

    Read more

  • Teaching and acting (or, why I don’t plan to sell recorded classes in the near future)

    Several weeks ago, my wife and I saw a wonderful play at our local theater in Modi’in  (“Mother Courage and Her Children“).  At the end, the actors came out to receive their richly deserved applause.  Three times, the actors came out, took their bows, and were warmly applauded by the audience.  We loved their performance…

    Read more

  • Benchmarking old-style and new-style Python classes

    It has been many years since Python developers were really supposed to worry about new-style vs. old-style classes.  There is only one style (new) in Python 3.x, and even in Python 2.x, old-style classes have not been recommended for many years.  Nevertheless, I mention old-style classes in my Python courses, mostly so that participants will…

    Read more