79,869 questions
3292
votes
7
answers
2.9m
views
Understanding Python super() with __init__() methods [duplicate]
Why is super() used?
Is there a difference between using Base.__init__ and super().__init__?
class Base(object):
def __init__(self):
print "Base created"
class ChildA(...
2531
votes
16
answers
1.6m
views
How can I check if an object has an attribute?
How do I check if an object has some attribute? For example:
>>> a = SomeClass()
>>> a.property
Traceback (most recent call last):
File "<stdin>", line 1, in <...
1945
votes
67
answers
4.8m
views
What does "Could not find or load main class" mean?
A common problem that new Java developers experience is that their programs fail to run with the error message: Could not find or load main class ...
What does this mean, what causes it, and how ...
1357
votes
27
answers
1.1m
views
What is the purpose of the `self` parameter? Why is it needed?
Consider this example:
class MyClass:
def func(self, name):
self.name = name
I know that self refers to the specific instance of MyClass. But why must func explicitly include self as a ...
1132
votes
8
answers
278k
views
What is the difference between old style and new style classes in Python?
What is the difference between old style and new style classes in Python? When should I use one or the other?
979
votes
16
answers
501k
views
What is a clean "pythonic" way to implement multiple constructors?
I can't find a definitive answer for this. As far as I know, you can't have multiple __init__ functions in a Python class. So how do I solve this problem?
Suppose I have a class called Cheese with the ...
851
votes
13
answers
1.5m
views
How to print instances of a class using print()?
When I try to print an instance of a class, I get an output like this:
>>> class Test():
... def __init__(self):
... self.a = 'foo'
...
>>> print(Test())
<__main__....
795
votes
17
answers
388k
views
What is the difference between __init__ and __call__? [duplicate]
I want to know the difference between __init__ and __call__ methods.
For example:
class test:
def __init__(self):
self.a = 10
def __call__(self):
b = 20
750
votes
13
answers
251k
views
Difference between object and class in Scala
I'm just going over some Scala tutorials on the Internet and have noticed in some examples an object is declared at the start of the example.
What is the difference between class and object in Scala?
678
votes
11
answers
540k
views
When to use static classes in C# [duplicate]
Here's what MSDN has to say under When to Use Static Classes:
static class CompanyInfo
{
public static string GetCompanyName() { return "CompanyName"; }
public static string GetCompanyAddress()...
636
votes
16
answers
339k
views
Can we instantiate an abstract class?
During one of my interview, I was asked "If we can instantiate an abstract class?"
My reply was "No. we can't". But, interviewer told me "Wrong, we can."
I argued a bit on this. Then he told me to ...
574
votes
42
answers
431k
views
Private properties in JavaScript ES6 classes
Is it possible to create private properties in ES6 classes?
Here's an example.
How can I prevent access to instance.property?
class Something {
constructor(){
this.property = "test";
}
}
...
563
votes
14
answers
390k
views
How to read the value of a private field from a different class in Java?
I have a poorly designed class in a 3rd-party JAR and I need to access one of its private fields. For example,
why should I need to choose private field is it necessary?
class IWasDesignedPoorly {
...
544
votes
17
answers
530k
views
ES6 class variable alternatives
Currently in ES5 many of us are using the following pattern in frameworks to create classes and class variables, which is comfy:
// ES 5
FrameWork.Class({
variable: 'string',
variable2: true,
...
535
votes
7
answers
432k
views
How to invoke the super constructor in Python?
class A:
def __init__(self):
print("world")
class B(A):
def __init__(self):
print("hello")
B() # output: hello
In all other languages I've worked with the super constructor ...
514
votes
29
answers
408k
views
What are the differences between struct and class in C++?
This question was already asked in the context of C#/.Net.
Now I'd like to learn the differences between a struct and a class in C++. Please discuss the technical differences as well as reasons for ...
491
votes
9
answers
202k
views
Static vs class functions/variables in Swift classes?
The following code compiles in Swift 1.2:
class myClass {
static func myMethod1() {
}
class func myMethod2() {
}
static var myVar1 = ""
}
func doSomething() {
myClass....
475
votes
9
answers
177k
views
Difference between a class and a module
I came from Java, and now I am working more with Ruby.
One language feature I am not familiar with is the module. I am wondering what exactly is a module and when do you use one, and why use a ...
459
votes
19
answers
622k
views
How can I create an object and add attributes to it?
I want to create a dynamic object in Python and then add attributes to it. This didn't work:
obj = object()
obj.somefield = "somevalue"
AttributeError: 'object' object has no attribute '...
441
votes
7
answers
411k
views
Difference between 'cls' and 'self' in Python classes?
Why is cls sometimes used instead of self as an argument in Python classes?
For example:
class Person:
def __init__(self, firstname, lastname):
self.firstname = firstname
self....
428
votes
20
answers
436k
views
Declaring static constants in ES6 classes?
I want to implement constants in a class, because that's where it makes sense to locate them in the code.
So far, I have been implementing the following workaround with static methods:
class MyClass ...
383
votes
10
answers
1.2m
views
Using two CSS classes on one element
What am I doing wrong here?
I have a .social div, but on the first one I want zero padding on the top, and on the second one I want no bottom border.
I have attempted to create classes for this ...
374
votes
19
answers
645k
views
What is the difference between private and protected members of C++ classes?
What is the difference between private and protected members in C++ classes?
I understand from best practice conventions that variables and functions which are not called outside the class should be ...
367
votes
2
answers
520k
views
correct way to define class variables in Python [duplicate]
I noticed that in Python, people initialize their class attributes in two different ways.
The first way is like this:
class MyClass:
__element1 = 123
__element2 = "this is Africa"
def ...
355
votes
22
answers
450k
views
Python function overloading
I know that Python does not support method overloading, but I've run into a problem that I can't seem to solve in a nice Pythonic way.
I am making a game where a character needs to shoot a variety of ...