Function to create new objects of a class?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 649 Views - Last Post: 02 December 2012 - 03:19 PM Rate Topic: -----

#1 cb1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-November 12

Function to create new objects of a class?

Posted 02 December 2012 - 11:03 AM

Hello everyone, just wondering how to go about writing a function that takes string arguments and returns an object of a class, for example:

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

   def show(P):
      print(P.firstName,P.lastName)

def stringToObject(string):
   string = string.split()
   fName = string[0]
   lName = string[1]
   return Person(fName,lName)
   


Also how would I access this object? I know if for example I created an object using: Example = Person("John","Smith"), I could access this using John.show() but I have no idea how to access the objects that the function creates, or if the code is even working, or if its the right way to go about it. Any help much appreciated as always.

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Function to create new objects of a class?

#2 cb1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-November 12

Re: Function to create new objects of a class?

Posted 02 December 2012 - 11:18 AM

Edit, When I run code:
Name = stringToObject("John","smith")


I get the error:
Name.show()
AttributeError: 'NoneType' object has no attribute 'show'

So im guessing my code isnt even creating a new object correctly?

Sorry and then Name.show()

Cant see an edit button anywhere
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2133
  • View blog
  • Posts: 3,269
  • Joined: 21-June 11

Re: Function to create new objects of a class?

Posted 02 December 2012 - 11:57 AM

When you run Name = stringToObject("John", "smith"), you should get an error right there. You're calling stringToObject with 2 arguments, but it only takes one.
Was This Post Helpful? 0
  • +
  • -

#4 cb1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-November 12

Re: Function to create new objects of a class?

Posted 02 December 2012 - 01:06 PM

View Postsepp2k, on 02 December 2012 - 11:57 AM, said:

When you run Name = stringToObject("John", "smith"), you should get an error right there. You're calling stringToObject with 2 arguments, but it only takes one.


Thanks for your response, however thats not the problem, thats a little code I wrote as an example the problem im having is creating an object of the class Person, using details from a string, with a function, then how to access the newly created object.

As the function will create the object like
Person("john","smith")


instead of
John = Person("john...)

Which I know I could access using John.show(), but how would I access the the objects created by the function? As the function would be used in a loop to process a list of details.

Thanks to anyone who may have a solution!
Was This Post Helpful? 0
  • +
  • -

#5 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Function to create new objects of a class?

Posted 02 December 2012 - 01:23 PM

It is exactly as sepp2k said. Your problem is that your function stringToObject takes exactly one argument, but you gave it two.

Just try:
john_person = stringToObject("John Smith")
john_person.show()



Your function stringToObject does what it is supposed to do. It takes ONE string and returns a Person object. You only have to call it correctly.
Was This Post Helpful? 0
  • +
  • -

#6 cb1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-November 12

Re: Function to create new objects of a class?

Posted 02 December 2012 - 01:36 PM

View PostNallo, on 02 December 2012 - 01:23 PM, said:

It is exactly as sepp2k said. Your problem is that your function stringToObject takes exactly one argument, but you gave it two.

Just try:
john_person = stringToObject("John Smith")
john_person.show()



Your function stringToObject does what it is supposed to do. It takes ONE string and returns a Person object. You only have to call it correctly.


Thanks for your response, once again this isnt the problem, just an example. I wrote that in a rush, its just an example of my code, my code is alittle more complicated and would take a while putting up. However, all I need to do is know how to access the objects created by my function. and how to create objects from a string using a function.

def strToObj(string):
   string = string.split()
   fname = string[0]
   lname = string[1]
   return
   Person(fname,lname)



This code should take the string, then get the seperate elements to supply to create a new object Person with the details obtained from the string. You see?

So how do I refer to these newly created objects, and for some reason when I call the function the function isnt returning anything!

Once again thanks for replies I do appreciate it!
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2133
  • View blog
  • Posts: 3,269
  • Joined: 21-June 11

Re: Function to create new objects of a class?

Posted 02 December 2012 - 01:48 PM

In the code you've posted now line 5 will return nothing and line 6 will be dead code. If you merge lines 5 and 6 like you did in the code you've posted originally, everything will work fine and there will be no problem accessing the object afterwards.
Was This Post Helpful? 0
  • +
  • -

#8 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Function to create new objects of a class?

Posted 02 December 2012 - 01:50 PM

You have to bind the Object, that your function returns to a name. If you don't do that it is lost for good.

So
def make_my_object(some_arguments):
    #some computation
    return my_object

my_object = make_my_object(some_arguments)
#now you can do something with my_object



In your last code there is a problem though
def strToObj(string):
   string = string.split()
   fname = string[0]
   lname = string[1]
   return
   Person(fname,lname)



Person(fname,lname) has to be on the same line as the return statement. Otherwise the function will return None. What you want to return has to follow the return statement on the same line.
So:
def strToObj(string):
   string = string.split()
   fname = string[0]
   lname = string[1]
   return Person(fname,lname)


Was This Post Helpful? 0
  • +
  • -

#9 cb1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-November 12

Re: Function to create new objects of a class?

Posted 02 December 2012 - 01:53 PM

View Postsepp2k, on 02 December 2012 - 01:48 PM, said:

In the code you've posted now line 5 will return nothing and line 6 will be dead code. If you merge lines 5 and 6 like you did in the code you've posted originally, everything will work fine and there will be no problem accessing the object afterwards.


Thanks for the reply, but how would I access the objects. The actual code in my program will be inside a loop and processing a long list of attributes...

For example

111 john smith
211 dave simpson
210 dale trump

So I would be passing these strings through the function strToObj. So would it simply be 111.show()? How do I refer to these newly created objects? is it the first argument passed through the __init__ (eg. Person(111,"John","smith") would be 111.show())?
Was This Post Helpful? 0
  • +
  • -

#10 cb1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-November 12

Re: Function to create new objects of a class?

Posted 02 December 2012 - 02:11 PM

Also, my methods in my class are:

def show(s):
        print(s.name,s.jobTitle,s.payrollNo,s.salary)

    def name(s):
        print(s.name)

    def payroll(s):
        print(s.payrollNo)

    def jobtitle(s):
        print(s.jobTitle)

    def salary(s):
        print(s.salary)


the .show() method works fine, however, for some reason when I use .name() or .payroll() etc I get the following error:
strToObject(string).jobTitle()
TypeError: 'str' object is not callable


I dont understand why this is happening as im calling the print function to print the info stored at the job title attribute?
Was This Post Helpful? 0
  • +
  • -

#11 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Function to create new objects of a class?

Posted 02 December 2012 - 02:25 PM

Look at the indentation. All the other methods are indented more than the show method. They shouldn't be.

Also, having methods with the same names as variables is a bad idea (if it's even allowed, I'm not sure...). The thing is that Python uses the same namespace for variables and functions.

Now, to your main problem. You have a function that returns an object:
def some_function(some_parameter):
    # blah blah
    return some_object


Whenever you call that function, you get a newly created object, that you can store in a variable, and then access.
some_obj = some_function("blah blah")


Or, to use your own example
John = strToObj("John Smith")
John.show()


Is exactly the same as
John = Person("John","Smith")
John.show()


Was This Post Helpful? 0
  • +
  • -

#12 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2133
  • View blog
  • Posts: 3,269
  • Joined: 21-June 11

Re: Function to create new objects of a class?

Posted 02 December 2012 - 02:30 PM

Your object has a string variable called jobTitle and a method called jobtitle. You're accessing the variable, not the method, and then try to call it like a function.

View PostTayacan, on 02 December 2012 - 10:25 PM, said:

Also, having methods with the same names as variables is a bad idea (if it's even allowed, I'm not sure...).


It's not allowed, but his variables and methods don't actually have the same names - they differ in case. Of course that's a bad idea as well as it's damn confusing.
Was This Post Helpful? 1
  • +
  • -

#13 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Function to create new objects of a class?

Posted 02 December 2012 - 02:32 PM

View Postsepp2k, on 02 December 2012 - 09:30 PM, said:

It's not allowed, but his variables and methods don't actually have the same names - they differ in case. Of course that's a bad idea as well as it's damn confusing.


...and I believe I just proved your point by getting confused by it myself. :P
Was This Post Helpful? 0
  • +
  • -

#14 cb1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-November 12

Re: Function to create new objects of a class?

Posted 02 December 2012 - 02:46 PM

Thanks for responses everyone, I have now solved the original problem, just onto the type error!

The indentation was from the way I typed it out on here, in the code the indentation is fine.

Im running these:
string = "12345 35000 consultant Bart Simpson"
strToObject(string).show()
strToObject(string).name()
strToObject(string).payrollno()
strToObject(string).jobtitle()
strToObject(string).salary()


My class def's:
  def show(s):
        print(s.name,s.jobTitle,s.payrollNo,s.salary)

    def name(s):
        print(s.name)

    def payroll(s):
        print(s.payrollNo)

    def jobtitle(s):
        print(s.jobTitle)

    def salary(s):
        print(s.salary)


The output:
Bart Simpson consultant 12345 35000
Traceback (most recent call last):
  File "*******************************.py", line 55, in <module>
    strToObject(string).name()
TypeError: 'str' object is not callable


So the first one, .show() is working fine, however the others are giving the type error?
Was This Post Helpful? 0
  • +
  • -

#15 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Function to create new objects of a class?

Posted 02 December 2012 - 02:58 PM

Yes, okay, here I do believe that the issue is that you have a method and a variable that are both called name. Try changing the method to getName().
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2