How to pass multiple value to the class?

Asked
Active3 hr before
Viewed126 times

4 Answers

multiplevalue
90%

If you want use a function that gets two parameters in a class. You can use my simple code:,When you want to pass initialisation parameters you just need to pass them within the __init__ function as you were doing., Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,It might be a good idea though, to store these values as a dictionary if they're for different uses, as that's easier to access than a plain list. You can turn the *args into a dictionary pretty easily if you pass every argument as a 2 element tuple like this:

This line:

class doWorkWithItems(dueX, dueY, dueZ, ...):

When you want to pass initialisation parameters you just need to pass them within the __init__ function as you were doing.

class doWorkWithItems(object):
   def __init__(self, dueX, dueY, dueZ, ..):

As for the best way to have a long list of arguments, Python has an operator for that, it's *. It unpacks a collection of items. It's commonly used with the name args and will allow for any amount of parameters to be passed in.

    def __init__(self, * args):
       self.args = args

It might be a good idea though, to store these values as a dictionary if they're for different uses, as that's easier to access than a plain list. You can turn the *args into a dictionary pretty easily if you pass every argument as a 2 element tuple like this:

    def __init__(self, * args):
       try:
       self.args = dict(args)
    except TypeError:
       #What to do
          if invalid parameters are passed

       ...

       obj = doWorkWithItems(("Name", "John"), ("Age", 45), ("Occupation", "Professional Example"), )
    obj.args >>>
       {
          'Age': 45,
          'Name': 'John',
          'Occupation': 'Professional Example'
       }

But there is a better approach you could do if you pass parameters in a dictionary:

    def __init__(self, params):
       self.name = params.get('name')
    self.age = params.get('age')
    self.occupation = params.get('occupation')
load more v
88%

Python program to convert a list to string,In the above program, the displayMessage() function is called without passing any arguments to it.,Here is a program to illustrate all the above cases to pass multiple arguments in a function. ,sum() function in Python

load more v
72%

function friends() {
   var names = {
      closeCircle: ['bebo', 'eli', 'matthew'],
      outerCircle: ['vierie', 'nate', 'james', 'george'],
   };
   return [names.closeCircle[0], names.closeCircle[2]]
   //returns bebo and matthew
}
load more v
65%

We can use both the options to pass multiple parameters to a GET method as follows:,What are the ways to pass multiple parameters to a GET method?,Passing multiple parameters using FromQuery,There are more ways to pass multiple parameters to method, but following are the options mostly used for GET method:

Example:

//GET method
//single parameter
public IActionResult Get(int id)

//multiple parameter
[HttpGet("{id}/{backendOnly}")]
public IActionResult Get(int id, string backendOnly)

//multiple parameters
[HttpGet("{id}/{first}/{second}")]
public IActionResult Get(int id, string first, string second)
//GET request using route data
https: //localhost:44363/2
   https: //localhost:44363/2/first
   https: //localhost:44363/2/first/second

In this option the parameters are bound using the request query string.

Example:

//GET method
//single parameter
[HttpGet("details")]
public IActionResult Details(int id)

//multiple parameter
[HttpGet("details")]
public IActionResult Details(int id, string first)

//multiple parameters
[HttpGet("details")]
public IActionResult Details(int id, string first, string second)
//GET request using query parameters
https: //localhost:44363/details?id=2
   https: //localhost:44363/details?id=2&&first=csharp
   https: //localhost:44363/details?id=2&&first=csharp&&second=mvc
Example:
[BindProperties]
public class GetRequest {
   public int Id {
      get;
      set;
   }
   public string FrontEnd {
      get;
      set;
   }
   public string BackEnd {
      get;
      set;
   }
}
Example:
[BindProperty]
public int Id {
   get;
   set;
}

For example, we are passing multiple parameters to GET method but in case our parameters exceed three, then it’s not readable and best practice to use that way. We can use class model as a parameter to a GET method with model binding as follows:

// Model as parameter
[BindProperties]
public class GetRequest {
   public int Id {
      get;
      set;
   }
   public string FrontEnd {
      get;
      set;
   }
   public string BackEnd {
      get;
      set;
   }
}
// GET method
[HttpGet("details")]
public IActionResult GetAction(GetRequest getRequest)
https: //localhost:44363/details?id=2&&frontend=angular&&backend=dotnet

We can use both the options to pass multiple parameters to a GET method as follows:

// GET method
[HttpGet("id")]
public IActionResult BothDetails(int id, string backendOnly, string frontendOnly)
// GET request using both ways
https: //localhost:44363/3?backendOnly=csharp&&frontendOnly=angular
load more v

Other "multiple-value" queries related to "How to pass multiple value to the class?"