Python Forum
Use of classes or functions discussion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of classes or functions discussion
#1
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')
Reply
#2
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?
Reply
#3
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.
Reply
#4
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.)
Reply
#5
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)
Reply
#6
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.
Reply
#7
But is there a reason other than a personal one?
What is the Python overhead - I guess that's what I'm asking
Reply
#8
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.
Reply
#9
This video is quite popular. I think it tells everything
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  discussion: API proposal for a better numpy jimy_byerley 0 1,472 Jul-19-2020, 08:56 PM
Last Post: jimy_byerley
  ok to post functions/classes/modules? Skaperen 2 2,964 Jul-03-2018, 12:22 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020