How to pass an input argument when creating an instance of a class?
Asked Answered
C

8

43

My idea was to implement the following class:

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?

Consolidate answered 15/6, 2016 at 14:0 Comment(3)
Why do you think it is not possible? start from here: docs.python.org/3/tutorial/classes.html#a-first-look-at-classesMarozas
sopython.com/wiki/What_tutorial_should_I_read%3F - any introductory Python tutorial will cover this kind of basic OOP.Attaway
@Attaway - your link redirects to 404, here is the repost: sopython.com/wiki/What_tutorial_should_I_read%3FFowler
R
45
>>> class name(object):
...     def __init__(self, name):
...         self.name = name
... 
>>> person1 = name("jean")
>>> person2 = name("dean")
>>> person1.name
'jean'
>>> person2.name
'dean'
>>>
Romine answered 15/6, 2016 at 14:4 Comment(0)
S
77

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)

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.

For example:

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

a = name('bob')
a.PrintName()
bob
Saeger answered 15/6, 2016 at 14:10 Comment(0)
R
45
>>> class name(object):
...     def __init__(self, name):
...         self.name = name
... 
>>> person1 = name("jean")
>>> person2 = name("dean")
>>> person1.name
'jean'
>>> person2.name
'dean'
>>>
Romine answered 15/6, 2016 at 14:4 Comment(0)
A
12

You just need to do it in correct syntax. Let me give you a minimal example I just did with Python interactive shell:

>>> class MyNameClass():
...   def __init__(self, myname):
...       print myname
... 
>>> p1 = MyNameClass('John')
John
Alton answered 15/6, 2016 at 14:5 Comment(0)
L
7

Remove the name param from the class declaration. The __init__ method is used to pass arguments to a class at creation.

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

me = Person("TheLazyScripter")
print me.name
Ligroin answered 15/6, 2016 at 14:5 Comment(0)
J
2

Python Classes

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

Somewhere else:

john = name("john")

Output:
name: john

Jaye answered 15/6, 2016 at 14:7 Comment(0)
T
2

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)

Output:

jean
dean
===
jean
dean
Toxic answered 29/2, 2020 at 14:9 Comment(0)
U
1

class Person:

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

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

print(p1.name)

print(p1.place)

Output

Muthuswamy

India
Usm answered 13/10, 2019 at 2:37 Comment(0)
B
-1

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)

Then

a = Onoma("MN")
a.name

returns

'MN'
Bostick answered 23/12, 2023 at 23:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.