This question already has answers here:
What is the purpose and use of **kwargs? [duplicate]
(13 answers)
Closed 7 years ago.
I'm testing to use kwargs and have a trouble.
This is the python3 code :
class B:
def __init__(self, **kwargs):
print(kwargs['city'])
a = {'phone':'0101', 'city':'Daejeon'}
b = B(a)
But, there is an error like below :
b = B(a)
TypeError: __init__() takes 1 positional argument but 2 were given
Is there something wrong in my code?
I think that I just exactly follow the tutorial....
Keyword arguments are not passed that way.
obj1 = B(phone='0101', city='Daejeon')
obj2 = B(**a)
Related
This question already has answers here:
What is the purpose of the `self` parameter? Why is it needed?
(26 answers)
Closed 3 years ago.
In this program the output is not what I would expect:
class example(object):
def __init__(self, foo,bar):
self.foo = foo
self.bar = bar
def what_are_you():
print(foo,bar)
print("This doesn't work either")
a = example("aaa","bbb")
a.what_are_you
#should return "aaa bbb"
print(a.what_are_you)
print(a.foo,a.bar)
Instead it outputs nothing. This is the whole output:
<bound method example.what_are_you of <__main__.example object at 0x000002A9574B4710>>
aaa bbb
Process returned 0 (0x0) execution time : 0.041 s
By not including parentheses, you're printing the function object, not calling the function. To call the function, put parentheses after it.
print(a.what_are_you())
will print the value of what_are_you() (whatever is in the return statement)
As I see now, you're printing stuff, not returning stuff, so you might need to use:
a.what_are_you()
And in your function, you need to use self to get the variable:
def what_are_you(self):
print(self.foo,self.bar)
print("This doesn't work either")
This question already has answers here:
TypeError: Missing 1 required positional argument: 'self'
(8 answers)
Closed 1 year ago.
I have two classes,in the first one I create an instance of the second class and I execute a method of the second class, a method that starts a process.
t1.py
test = "t1"
def executeBase():
base = baseNode.BaseNode()
baseNode.BaseNode.executeBase(test, base) #error
and baseNode.py
class BaseNode():
def __init__(self):
self.eui48 = "01:00:00:00:00:00"
self.port = 7919
def executeBase(self, test, base):
#I execute here a process
I got the error in line (#error).
File "/testbench/testbenchPython/test/t1.py", line 20, in executeBase
baseNode.BaseNode.executeBase(test, base)
TypeError: executeBase() missing 1 required positional argument: 'base'
Isn't it possible to do this? If yes, what's the problem and how can I correct it?
I've tried passing the arguments in different ways but I didn't find the solution.
Thank you very much!
You should call executeBase on your instance.Try this:
def executeBase():
base = baseNode.BaseNode()
base.executeBase(test, base)
In this case
def executeBase():
base = baseNode.BaseNode()
baseNode.BaseNode.executeBase(test, base) #error
I'm guessing executeBase() func looking for self! instead of baseNode.BaseNode.executeBase(test, base) #error this line this may work base.executeBase(test,base)
This question already has answers here:
How can I pair socks from a pile efficiently?
(38 answers)
Python functions with multiple parameter brackets
(3 answers)
Syntax of Keras Functional API
(2 answers)
Closed 4 years ago.
I know that 'X' on right-hand side is the input before you applied dropout. Now when you say Dropout(0.5)(X), it means that you are randomly making the activation of 50% of the neurons in the input to zero.
This line Dropout(0.5) means which are making a new object of class Dropout and passing 0.5 in its constructor. But appending X at the end of it? What does this mean in Python Syntax?
There are multiple ways that this code can work, 2 come into my mind right now:
Using the built in __call__ function of classes
>>> class MyClass:
... def __init__(self, name):
... self.name = name
... def __call__(self, word):
... print(self.name, 'says', word)
...
>>> MyClass('Tom')('hello')
Tom says hello
A function returning a function
>>> def add(a):
... def f2(b):
... print(a+b)
... return f2
...
>>> add(1)(2)
3
I hope this helps
This question already has answers here:
How can I represent an 'Enum' in Python?
(43 answers)
Closed 8 years ago.
I have declared the enum as follows in python.I don't know how to use them.When I create an instance of this class it gives error as two arguments are required one given.
class CBarReference(Enum):
ThisBar = 0,
NextBar = 1,
Undefined=2
a=CBarReference()
I know what error is but I don't know what to give as the second argument other than self.
You should never have to create an instance of an enum; they're all accessed directly from the class, and you can just assign them to variables as you like:
a = CBarReference.ThisBar
b = CBarReference.NextBar
c = CBarReference.Undefined
d = CBarReference.ThisBar
assert(a == d)
assert(b != a)
assert(b != c)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
Default values for arguments
I'm having trouble explaining how a list belonging to a class behaves.
>>> class A(object):
... def __init__(self, L=[]):
... self.L = L
...
>>> a1 = A()
>>> a2 = A()
>>> a1.L.append("test")
>>> print a1.L
['test']
>>> print a2.L
['test']
In this example, how did a2.L get a value in it? I only added an item to the list in a1. How is it that they're sharing the list now.
Thanks,
Henry
A brief summary of all the comments you will get:
The problem is nothing to do with the class. It's because you're storing the values in the default argument of a function, and default arguments are stored on the function objects.