![]() |
Use of classes or functions discussion - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: News and Discussions (https://python-forum.io/forum-31.html) +--- Thread: Use of classes or functions discussion (/thread-464.html) Pages:
1
2
|
Use of classes or functions discussion - Larz60+ - Oct-12-2016 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') RE: Test internet connection - micseydel - Oct-12-2016 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? RE: Test internet connection - Larz60+ - Oct-12-2016 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. RE: Test internet connection - micseydel - Oct-12-2016 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.) RE: Test internet connection - Larz60+ - Oct-12-2016 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 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) RE: Test internet connection - micseydel - Oct-12-2016 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. RE: Test internet connection - Larz60+ - Oct-12-2016 But is there a reason other than a personal one? What is the Python overhead - I guess that's what I'm asking RE: Test internet connection - micseydel - Oct-12-2016 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. RE: Test internet connection - wavic - Oct-12-2016 This video is quite popular. I think it tells everything RE: Test internet connection - Larz60+ - Oct-12-2016 This video is not ranked too highly by some. See https://www.quora.com/What-is-your-opinion-on-Jack-Diederichs-talk-of-stop-writing-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. |