-1

The code bellow in c++ is working, Its possible to make this works with python without using Users().Users(23, "bell") but like c++ Users(23, "bell") see my code bellow

#include <iostream>
using namespace std;


class Users
{
public:
    int age;
    string name;

public:
    Users()
    {
        // init default 
        age = 90;
        name = "john";

    }

    Users(int iage, string iname)
    {
        age = iage;
        name = iname;
    }
};


int main()
{
    Users User;
    User.age = 2;
    User.name = "l";

    Users(23, "bell");
    return 0;
}

''

class Users:
    age = None
    name = None 

    def __init__(self):
        self.age = 90
        self.name = "john"

    def Users(age, name):
        self.age = age
        self.name = name

User = Users()
User.age = 2
User.name = "l"
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
ergesto
  • 367
  • 1
  • 8
  • .... that's what the `__init__` function's there for. `__init__` is python's constructor. just add the arguments there and remove your `User` method, it has no special meaning in Python. – Nearoo Jul 11 '18 at 12:06
  • Have you read the basic tutorial? Your actual question is unclear but I'm pretty sure you're trying to add arguments to `__init__`. – Mad Physicist Jul 11 '18 at 12:09
  • no i dont want to add arg to __init__ – ergesto Jul 11 '18 at 12:10
  • 1
    Yes you do. Function (or constructor) overloading is done with default arguments in python. – Aran-Fey Jul 11 '18 at 12:12
  • 2
    Please don't edit the solution into your question. Solutions don't belong in the question, they should be posted as answers. – Aran-Fey Jul 11 '18 at 12:22
  • @chepner What was wrong with that dupe? – Aran-Fey Jul 11 '18 at 12:25
  • Nothing in particular, but the OP had enough misconceptions that I think he needed a custom answer. – chepner Jul 11 '18 at 12:25
  • Related/dupe: [What is a clean, pythonic way to have multiple constructors in Python?](//stackoverflow.com/q/682504) – Aran-Fey Jul 11 '18 at 12:26
  • (I'm willing to be flagged for my probable abuse of dup-hammer reopening powers :)) – chepner Jul 11 '18 at 12:26
  • @chepner If you think there's value in reopening the question, fine - but I honestly don't see how your answer is an improvement over the answer in the dupe. Did you address any of those misconceptions? – Aran-Fey Jul 11 '18 at 12:29
  • @Aran-Fey I fixed the class name and variable name conventions, got rid of the class attributes and unnecessary methods, and provided several examples of how to use the single constructor. – chepner Jul 11 '18 at 12:45

1 Answers1

2

Use default arguments to provide values when you don't want to pass a default value explicitly.

class User:  # singular noun
    def __init__(self, age=90, name="john"):
        self.age = age
        self.name = name

user1 = User()            # user.age == 90, user.name == "john"
user2 = User(2, "l")      # user.age == 2, user.name == "l"
user3 = User(40)          # user.age == 40, user.name == "john"
user4 = User(name="bob")  # user.age == 90, user.name == "john"

Since the unspecified arguments are replaced by defaults in strict left-to-right order, the last example shows the use of a keyword argument to set the name explicitly while using the default age.

chepner
  • 497,756
  • 71
  • 530
  • 681