-2

I have the following function where I will usually pass as arguments Vm and Vp, hoewever, in some cases I would like to add as well as an input parameter a numpy.array, called Yin.

def solveTransmissionLineDistrbutedE(self,Vm,Vp,*,Yin=0):

In that case, I am facing up an issue when calling my function with Yin as a numpy.arry like the following one

ec_0_04 = numpy.array([
                      [0.00188453428035827 + 0.0124200286145695j],
                      [0.00188453428035827 + 0.0124200286145695j]
                      ])
for yin in ec_0_04:
        EC,C = pinsAsLine.solveTransmissionLineDistrbutedE(-1,-1,*yin)

Then, Python complains with this error

    EC,C = pinsAsLine.solveTransmissionLineDistrbutedE(-1,-1,numpy.array([0.00188453428035827 + 0.0124200286145695j]))
TypeError: solveTransmissionLineDistrbutedE() takes 3 positional arguments but 4 were given

I do understand the error, Python see four inputs to a function of three, but I am not sure how to properly pass my array as input to the function.

Any hints?

Thanks in advance,

ndarkness
  • 1,011
  • 2
  • 16
  • 36
  • bite me if i am wrong, but i think pinsAsLine is the 4th (or rather first) argument you pass. If solveTransmission.... is your method, add a 'this' before the other parameters – bv_Martn Jun 17 '19 at 13:11
  • Possible duplicate of [How do I create a Python function with optional arguments?](https://stackoverflow.com/questions/9539921/how-do-i-create-a-python-function-with-optional-arguments) – TomNash Jun 17 '19 at 13:11
  • 1
    Code does not match error message… – deceze Jun 17 '19 at 13:11
  • `solveTransmissionLineDistrbutedE(-1,-1,yin)`? – Stop harming Monica Jun 17 '19 at 13:17

2 Answers2

3

You are misunderstanding the use of the *.

You declared your method like this:

def solveTransmissionLineDistrbutedE(self,Vm,Vp,*,Yin=0):

Which means your function will take self as first arg, Vm and Vp as second and third. They are positional arguments. Then it will accept a named argument (or keyword-only argument): Yin.

In the end, you should call your method like this:

for yin in ec_0_04:
     EC,C = pinsAsLine.solveTransmissionLineDistrbutedE(-1,-1,Yin=yin)

By the way, I may be wrong but I don't think the * to be of any use in your method declaration.

olinox14
  • 6,177
  • 2
  • 22
  • 39
  • by doing so I fixed the issue with the caller, but now internally in `solveTransmissionLineDistrbutedE` I make use of `fsolve` which complains as `TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument 'CandGTransmissionLineDistributedE'.Shape should be (2,) but it is (2, 1).` How can I solve this? – ndarkness Jun 17 '19 at 13:21
  • I won't be able to tell without knowing what's inside your function... You should also know that you are giving `0` as a delault value for yin, but pass an array: this will probably causes you problems in the future – olinox14 Jun 17 '19 at 13:25
  • The function inside has a call like this `g,c = fsolve(self.CandGTransmissionLineDistributedE,self.initialGoCo,Yin)` where initialGoCo is an numpy.array of two elements – ndarkness Jun 17 '19 at 13:31
  • 1
    I don't know... Maybe you should post another question about it? – olinox14 Jun 17 '19 at 13:32
1
for yin in ec_0_04:
        EC,C = pinsAsLine.solveTransmissionLineDistrbutedE(-1,-1,Yin=yin)

Pass the whole array as an argument.

micric
  • 621
  • 4
  • 15