DEV Community

Cover image for Dead Simple Python: Classes

Dead Simple Python: Classes

Jason C. McDonald on January 19, 2019

Like the articles? Buy the book! Dead Simple Python by Jason C. McDonald is available from No Starch Press. Classes and objects: the bread-and-...
Collapse
 
rpalo profile image
Ryan Palo

I read a really funny analogy for why python’s private-ish variables are good enough and we don’t have stricter access control:

I’d like you to stay out of my living room because I ask you to, not because I have a shotgun!

Thanks for the article!

Collapse
 
akashsharma02 profile image
Akash Sharma • Edited

I'd like to add that you should mention that there's pretty much never a good reason to use staticmethod as opposed to a global method in the module.

Usually, when we write a module, we can simply write a method outside the class, to get the same behaviour as a staticmethod

Collapse
 
codemouse92 profile image
Jason C. McDonald

I try to avoid such statements, especially in this article series. If it exists, it exists for some reason. ;)

That said, I plan to discuss the ins and outs of this in more depth in the book.

Collapse
 
akashsharma02 profile image
Akash Sharma

Ah I see! I really appreciate this series. It is very well written and enjoyable to read!

By the way, what is that reason?

Thread Thread
 
codemouse92 profile image
Jason C. McDonald

Namespacing.

Collapse
 
paulboot profile image
Paul Boot

Thanks for the excellent Article.

Some small suggestion: in the setter example the actual line of code that sets the Instance Member self.__captain = value is missing ;-)

class Starship:
    def __init__(self):
        # snip
        self.__captain = "Jean-Luc Picard"

    @property
    def captain(self):
        return self.__captain

    @captain.setter
    def captain(self, value):
        print("What do you think this is, " + value + ", the USS Pegasus? Back to work!")

uss_enterprise = Starship()
print(uss_enterprise.captain)
uss_enterprise.captain = "Wesley"
print(uss_enterprise.captain)

Resulting in this output, Jean-Luc Picard refuses to go away....

uss_enterprise = Starship()
uss_enterprise.captain
>>> 'Jean-Luc Picard'
uss_enterprise.captain = "Wesley"
>>> What do you think this is, Wesley, the USS Pegasus? Back to work!
uss_enterprise.captain
>>> 'Jean-Luc Picard'
Collapse
 
wangonya profile image
Kelvin Wangonya • Edited

@staticmethod
def make_sound(cls):

Should cls be passed into a staticmethod? I thought that was only for class methods

Collapse
 
codemouse92 profile image
Jason C. McDonald • Edited

I cannot believe it! I actually missed that little detail: @staticmethod and @classmethod are actually two separate things!

I've just edited the article to correct that, and added a new section to clarify the difference. Thank you so much for correcting that.

Collapse
 
wangonya profile image
Kelvin Wangonya

Great article!

Collapse
 
ardunster profile image
Anna R Dunster

Nice, properties, getters, and setters all explain some things I had never even seen or thought of before (Python being my first programming language), it definitely expanded my understanding of what a class is and can do.

Collapse
 
johnlukeg profile image
John Luke Garofalo

Great article! Python is neither my primary nor my secondary language, but I've been using it a lot for my grad school work. I realize how naive my own Python code has been after reading your post haha. Do you have any suggestions for more material like this to learn the best practices? I have found that the python docs are not the most captivating. Thank you for this post! Great job.

Collapse
 
codemouse92 profile image
Jason C. McDonald

Unfortunately, I haven't found much! That's precisely why I started this article series.

If you have some extra cash laying around, you could sign up for Udemy: The Complete Python Course by Codestars. He covers good practice pretty well. However, it is unfortunately quite beginner-oriented, so it's slow going at first.

Collapse
 
natter1 profile image
natter1
def engage(self, factor):

in the subtopic Methods should be

def warp(self, factor):

as engage has no parameter factor

Collapse
 
codemouse92 profile image
Jason C. McDonald

Another great catch. Thank you!

Collapse
 
ravin309 profile image
Ravinder Kumar

In inheritance, what if we do not write 'super().init()' in child class' init() method? Does child class not call it by default?

Collapse
 
codemouse92 profile image
Jason C. McDonald

No, it doesn't, as far as I know. As mentioned in the Zen of Python, Explicit is better than implicit. It's part of the language philosophy.

We often need to handle passing data to the parent __init__() in a specific way, and we may need to do some other things first, so Python just assumes we know best how to handle that.

Imagine how maddening it could get if Python sometimes called it for you, but sometimes not! (Some languages actually are confusing like that, but Python aims to be obvious.)

Collapse
 
ravin309 profile image
Ravinder Kumar

Thanks for clarifying!
It would be very helpful if you also explain MRO (Method Resolution Order) in context of Python 3 in any upcoming series.

Collapse
 
tinsoldier6 profile image
Jason Gade

This was a very clear and well-written article, probably one of the best that I’ve read regarding Python classes. Thanks!

Collapse
 
rupeshtiwari profile image
Rupesh Tiwari

Nice article Jason. Keep writing more.

Collapse
 
prathaprathod profile image
Prathap Rathod

Thanks for writing this article.Jason

Collapse
 
dextroza profile image
Domagoj Vukadin

Thank you for great article!
Just one thing, you have a typo in your example "engine_strain" as @property. It should be "elif" instead "else if".

Collapse
 
codemouse92 profile image
Jason C. McDonald

Thanks for catching that!

Collapse
 
paddy3118 profile image
Paddy3118

instance variables shadow (hide) the class variables,

Not quite. The class variables are shadowed; instance variables eclipse, as they dominate.

Good article, thanks 😎