Python 类输入参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37837682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python class input argument
提问by lars111
I am new to OOP. My idea was to implement the following class:
我是 OOP 的新手。我的想法是实现以下类:
class name(object, name):
def __init__(self, name):
print name
Then the idea was to create two instances of that class:
然后的想法是创建该类的两个实例:
person1 = name("jean")
person2 = name("dean")
I know, that is not possible, but how can I pass an input-argument into an instance of a class?
我知道,这是不可能的,但是如何将输入参数传递给类的实例?
回答by AbrahamB
The problem in your initial definition of the class is that you've written:
您对类的初始定义中的问题是您已经编写了:
class name(object, name):
This means that the class inherits the base class called "object", and the base class called "name". However, there is no base class called "name", so it fails. Instead, all you need to do is have the variable in the special initmethod, which will mean that the class takes it as a variable.
这意味着该类继承了名为“object”的基类和名为“name”的基类。但是,没有名为“name”的基类,因此失败。相反,您需要做的就是在特殊的init方法中拥有该变量,这意味着该类将其作为一个变量。
class name(object):
def __init__(self, name):
print name
If you wanted to use the variable in other methods that you define within the class, you can assign name to self.name, and use that in any other method in the class without needing to pass it to the method.
如果您想在类中定义的其他方法中使用该变量,您可以将 name 分配给 self.name,并在类中的任何其他方法中使用它,而无需将其传递给该方法。
For example:
例如:
class name(object):
def __init__(self, name):
self.name = name
def PrintName(self):
print self.name
a = name('bob')
a.PrintName()
bob
回答by joel goldstick
>>> class name(object):
... def __init__(self, name):
... self.name = name
...
>>> person1 = name("jean")
>>> person2 = name("dean")
>>> person1.name
'jean'
>>> person2.name
'dean'
>>>
回答by frlan
You just need to do it in correct syntax. Let me give you a minimal example I just did with Python interactive shell:
您只需要使用正确的语法即可。让我给你一个我刚刚用 Python 交互式 shell 做的最小例子:
>>> class MyNameClass():
... def __init__(self, myname):
... print myname
...
>>> p1 = MyNameClass('John')
John
回答by TheLazyScripter
Remove the name
param from the class declaration. The init method is used to pass arguments to a class at creation.
name
从类声明中删除参数。init 方法用于在创建时将参数传递给类。
class Person(object):
def __init__(self, name):
self.name = name
me = Person("TheLazyScripter")
print me.name
回答by dheiberg
class name:
def __init__(self, name):
self.name = name
print("name: "+name)
Somewhere else:
别的地方:
john = name("john")
john = name("john")
Output:name: john
输出:name: john
回答by Jay
How about this?
这个怎么样?
class name(str):
def __init__(self, name):
print (name)
# ------
person1 = name("jean")
person2 = name("dean")
print('===')
print(person1)
print(person2)
Output:
输出:
jean
dean
===
jean
dean
回答by Venkzz_venki
classPerson:
def init(self,name,age,weight,sex,mob_no,place):
self.name = str(name)
self.age = int(age)
self.weight = int(weight)
self.sex = str(sex)
self.mob_no = int(mob_no)
self.place = str(place)
类人:
def init(self,name,age,weight,sex,mob_no,place):
self.name = str(name)
self.age = int(age)
self.weight = int(weight)
self.sex = str(sex)
self.mob_no = int(mob_no)
self.place = str(place)
Creating an instance to class Person
创建类 Person 的实例
p1 = Person(Muthuswamy,50,70,Male,94*****23,India)
print(p1.name)
print(p1.place)
Output
输出
Muthuswamy
India