Say I have a variable, 'New', who's value is Raw_input("Enter name: ") How could I take this variable and pass it into a class?

Recommended Answers

All 4 Replies

Member Avatar for sravan953

What do you mean 'pass it to a class'? Can you post some code and pin-point your problem please?

You can pass value in as argument.

new = raw_input('Enter name: ')

class Test(object):
    def __init__(self,value):
        self.value = value

x = Test(new)
print x.value

'''
output-->
Enter name: hi
hi
'''

Not really recommended, since you don't really always want to pause your code but I think you want this:

class Name:
    def __init__(self):
        self.name = raw_input("Name: ")

However, I'd have to agree that snippsat's solution is a better written class, because if neccessary you could use it on names that you don't get through raw_input like...a list! For example:

#snippseat's class
class Test(object):
    def __init__(self,value):
        self.value = value

for name in namelist:
    test = Test(name)

Thanks, I can't see why I couldn't work that out!
neway, problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.