Skip to main content
Filter by
Sorted by
Tagged with
3292 votes
7 answers
2.9m views

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(...
Mizipzor's user avatar
  • 52.7k
2531 votes
16 answers
1.6m views

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 <...
Lucas Gabriel Sánchez's user avatar
1945 votes
67 answers
4.8m views

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 ...
Stephen C's user avatar
  • 723k
1357 votes
27 answers
1.1m views

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 ...
richzilla's user avatar
  • 42.3k
1132 votes
8 answers
278k views

What is the difference between old style and new style classes in Python? When should I use one or the other?
readonly's user avatar
  • 358k
979 votes
16 answers
501k views

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 ...
winsmith's user avatar
  • 21.7k
851 votes
13 answers
1.5m views

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__....
Ashwin Nanjappa's user avatar
795 votes
17 answers
388k views

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
sam's user avatar
  • 19.3k
750 votes
13 answers
251k views

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?
Steve's user avatar
  • 21.5k
678 votes
11 answers
540k views

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()...
pbh101's user avatar
  • 10.4k
636 votes
16 answers
339k views

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 ...
Ravi's user avatar
  • 31.6k
574 votes
42 answers
431k views

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"; } } ...
d13's user avatar
  • 10.1k
563 votes
14 answers
390k views

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 { ...
Frank Krueger's user avatar
544 votes
17 answers
530k views

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, ...
wintercounter's user avatar
535 votes
7 answers
432k views

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 ...
Mike's user avatar
  • 61.3k
514 votes
29 answers
408k views

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 ...
palm3D's user avatar
  • 8,340
491 votes
9 answers
202k views

The following code compiles in Swift 1.2: class myClass { static func myMethod1() { } class func myMethod2() { } static var myVar1 = "" } func doSomething() { myClass....
Senseful's user avatar
  • 92.6k
475 votes
9 answers
177k views

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 ...
Josh Moore's user avatar
  • 13.6k
459 votes
19 answers
622k views

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 '...
John's user avatar
  • 22.1k
441 votes
7 answers
411k views

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....
Scaraffe's user avatar
  • 5,281
428 votes
20 answers
436k views

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 ...
Jérôme Verstrynge's user avatar
383 votes
10 answers
1.2m views

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 ...
Francesca's user avatar
  • 28.4k
374 votes
19 answers
645k views

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 ...
Konrad's user avatar
  • 41.1k
367 votes
2 answers
520k views

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 ...
jeanc's user avatar
  • 4,873
355 votes
22 answers
450k views

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 ...
Bullets's user avatar
  • 3,591

1
2 3 4 5
1598