Is it not possible to define multiple constructors in Python? [duplicate]
Unlike Java, you cannot define multiple constructors. However, you can define a default value if one is not passed. def __init__(self, city=”Berlin”): self.city = city
Unlike Java, you cannot define multiple constructors. However, you can define a default value if one is not passed. def __init__(self, city=”Berlin”): self.city = city
Variable set outside __init__ belong to the class. They’re shared by all instances. Variables created inside __init__ (and all other method functions) and prefaced with self. belong to the object instance.
Hear it from the horse’s mouth. 🙂 From Bjarne Stroustrup’s C++ Style and Technique FAQ Why don’t we have virtual constructors? A virtual call is a mechanism to get work done given partial information. In particular, “virtual” allows us to call a function knowing only any interfaces and not the exact type of the object. … Read more
Python 3 includes an improved super() which allows use like this: super().__init__(args)
If your compiler supports C++11 standard, there is a constructor inheritance using using (pun intended). For more see Wikipedia C++11 article. You write: class A { public: explicit A(int x) {} }; class B: public A { using A::A; }; This is all or nothing – you cannot inherit only some constructors, if you write … Read more
Yes, throwing an exception from the failed constructor is the standard way of doing this. Read this FAQ about Handling a constructor that fails for more information. Having a init() method will also work, but everybody who creates the object of mutex has to remember that init() has to be called. I feel it goes … Read more
I’d say the Rule of Three becomes the Rule of Three, Four and Five: Each class should explicitly define exactly one of the following set of special member functions: None Destructor, copy constructor, copy assignment operator In addition, each class that explicitly defines a destructor may explicitly define a move constructor and/or a move assignment … Read more
No you can’t overload it; a static constructor is useful for initializing any static fields associated with a type (or any other per-type operations) – useful in particular for reading required configuration data into readonly fields, etc. It is run automatically by the runtime the first time it is needed (the exact rules there are … Read more
With only one level of inheritance, your example may not let you see the real benefits of Object.create. This methods allows you to easily implement differential inheritance, where objects can directly inherit from other objects. On your userB example, I don’t think that your init method should be public or even exist, if you call … Read more
I’d probably do something like this: <?php class Student { public function __construct() { // allocate your stuff } public static function withID( $id ) { $instance = new self(); $instance->loadByID( $id ); return $instance; } public static function withRow( array $row ) { $instance = new self(); $instance->fill( $row ); return $instance; } protected … Read more