Python Forum

Full Version: Use of classes or functions discussion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
This is not a foolproof way to test if internet is active or not, but I find that it works in at least 90% of the cases.
If someone has a better (as in works better) method, please post here.

import socket

class HasInternet:
    def __init__(self):
        self.ipaddress = socket.gethostbyname(socket.gethostname())
        self.isactive = self.ipaddress != '127.0.0.1'

if __name__ == '__main__':
    conn = HasInternet()
    if conn.isactive:
        print('Internet is active')
    else:
        print('Internet is inactive')
I think we've had this conversation before, but what benefit is there to the added complexity of a class and a method, rather than just a function?
A function is fine, if you wish
I import this in quite a few applications, and I prefer my imported files to be classes, C++ habit?
We have had this conversation on the old forum.
I just moved this over from the completed scripts.
That sounds more like a Java habit =p
A Python module not containing a class is totally fine. Classes have their place, but add complexity, so shouldn't be used by default. (Unless you're using Java, in which case you have no choice.)
I never really used Java for anything, other than maybe one or two fixes in my entire career.

C++ other than POD types (and even they can be classes), almost all code is encapsulated in a class.
I was introduced to C at Bell Labs, and resisted C++ for a long time, only getting comfortable with it in the early 1990's

About C++ class overhead:
Quote:With a compiler not written by drunk college students in the wee hours of the morning, the overhead is zero. At least until you start putting in
virtual functions; then you must pay the cost for the virtual dispatch mechanism. 'Karl Knechtel'

Actually, I used classes in C, even before C++ became popular. I just didn't know the methodology we used
was called classes.


Quote:Classes have their place, but add complexity

Is there a set of rules as to when to and when not to use them in Python? (I'm not being wiseacre)
I use a class if I need multiple instances of encapsulated state, and methods that go with that state. By default, I stick to regular procedural programming (or functional if that's the right thing in its context). I'll readily introduce a class if I have multiple different kinds of tuples, or the code gets big enough, but if the problem is solved by a single function I definitely haven't considered a class yet.
But is there a reason other than a personal one?
What is the Python overhead - I guess that's what I'm asking
I think Python overhead is a bad reason to worry about it. The reason to not use a class is that it is not necessary. It's the same reason you don't import sys, os, math, functools, etc. into scripts where they're not needed. There's a meager performance penalty for this, but it's mostly just that it's bad habit overcomplicate things.
This video is quite popular. I think it tells everything
This video is not ranked too highly by some. See https://www.quora.com/What-is-your-opini...ng-classes
This (almost) exact argument ran for several years after C++ came out

This will be my last word on this post.
I have found this code helpful in some GUI projects that I have worked on.
My intent on posting it was to be helpful.
This is something that new users face when they begin internet programming.
I never intended on opening Pandora's Box.
Pages: 1 2