English 中文(简体)
如何在设立一类案件时通过投入论点?
原标题:How to pass an input argument when creating an instance of a class?

我的想法是执行以下类别:

class name(object, name):
    def __init__(self, name):
        print name

之后,设想将出现两种情况:

person1 = name("jean")
person2 = name("dean")

我知道,这是不可能的,但我如何把意见论点纳入一个类别的例子?

最佳回答
>>> class name(object):
...     def __init__(self, name):
...         self.name = name
... 
>>> person1 = name("jean")
>>> person2 = name("dean")
>>> person1.name
 jean 
>>> person2.name
 dean 
>>>
问题回答

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 __init__ method, which will mean that the class takes it as a variable.

class name(object):
  def __init__(self, name):
    print name

如果你想要使用你在该类别内定义的其他方法中的变量,你可以把姓名指定给自己,并在该类别中采用任何其他方法,而无需通过该方法。

例如:

class name(object):
  def __init__(self, name):
    self.name = name
  def PrintName(self):
    print self.name

a = name( bob )
a.PrintName()
bob

你只是需要正确做。 让我给你一个起码的例子,我刚刚用斯堪的交互式手脚:

>>> class MyNameClass():
...   def __init__(self, myname):
...       print myname
... 
>>> p1 = MyNameClass( John )
John

将<代码>改为。 www.un.org/Depts/DGACM/index_french.htm 方法被用来在创作时将论点传给某类。

class Person(object):
  def __init__(self, name):
    self.name = name

me = Person("TheLazyScripter")
print me.name

rel=“nofollow”>Python Leveles

class name:
    def __init__(self, name):
        self.name = name
        print("name: "+name)

其他地方:

john = 姓名(“john”)

Output:
name: john

Actually you can! How about this?


class name(str):
    def __init__(self, name):
        print (name)
# ------
person1 = name("jean")
person2 = name("dean")
print( === )
print(person1)
print(person2)

产出:

jean
dean
===
jean
dean

<><><><>><>>>><>>>>>>>

def init(自称、名、年龄、体重、性别、莫布_no,名):

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

p1 = Person(Muthuswamy,50,70,Male,94*****23,India)

print(p1.name)

print(p1.place)

Output

Muthuswamy

India

It is a good idea to use different names for things that play different roles in the code. E.g. as in the following code:

class Onoma:
def __init__(self, dummy_name):
    self.name = dummy_name
    print(self.name)

之后

a = Onoma("MN")
a.name

回返

 MN 




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签