I a开发者_JAVA百科m writing a simple class that will basically accept 3 parameters, execute a series of commands and return the result.
Now the problem is that i am not clear about how to write a proper class in Python (i use C++ mainly); so is kinda weird for me to figure out what and how to write the class.
I have 3 parameters: name, classID and objectType (all 3 strings type), i need to return a string with the results of the operation.
So far I wrote this:
class testClass(superclass):
def __init__(self, name="",classID="", objectType="" *kwargs):
superclass.__init__(self, args, **kwargs)
result = ""
...(do the operation with the parameters, if the result is positive return OK
otherwise return KO)
Then i saved it as testClass.py and imported it in my main python program
The issue is that when i pass parameters i get an error saying that the global name "args" is not defined, on the superclass __init line
In C I would just write something like
class testClass
{
string name, classID, objectType;
}string
and the program would know that it must expect 3 parameters (and then deal in the constructor with the data verification and validation), but i cannot grasp how you tell Python the same thing.
If i can get to understand how to use Python in the way that it should be (and not translating code from a language to another), maybe i can avoid to get stuck like in this case :)
Any help is appreciate to shed some light on the subject.
I think what you are looking for is the function definition thats like this def __init__(self, name="", classId="", objectType="", *args, **kwargs):
.
What this defines besides the 3 known arguments (name, classId, objectType) is args which is a list of addtional arguments passed to the function, and kwargs which is a dictionary containing named arguments passed to the function.
With the above definition for your __init__
you could call your class this way:
t = testClass("name", "class1", "Object", "value1", 11, scope="local", destroy=True)
Now that call would give us inside __init__
name = "name"
classId= "class1"
objectType = "Object"
args = ["value1", 11]
kwargs = { 'scope': "local",
'destroy': True }
Oh and lastly, *args, **kwargs is optional for any function definition. If like you mention you know you will only be passed 3 arguments, then you can remove the *args, **kwargs.
If you want to require 3 parameters, try:
def __init__(self, name, classID, objectType):
Since you haven't supplied defaults they're required. In standard Python, there is no way to tell a function what type of arguments to expect -- just try to use them as strings and then errors will be raised if they're not compatible with string operations. If you really want to check, you can do:
if not isinstance(name, str):
raise TypeError
Do you actually need to call up to the superclass' __init__
? If so, use super()
:
super(testClass, self).__init__()
If you want to pass any unknown parameters after the three required ones up to the superclass' __init__
:
def __init__(self, name, classID, objectType, *args **kwargs):
super(testClass, self).__init__(*args, **kwargs)
*args
is collecting any extra non-keyword arguments into the variable args
. Notice **kwargs
has two stars -- it's collecting keyword arguments into a mapping / dictionary. There is nothing special about args
and kwargs
, you have to fill them in as above, they're not filled in magically.
精彩评论