I'm trying to develop a simple script that will keep the scores of a cricket match.
In my script I want the __init__ method on one class to create an object of another class.
here's an example.

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

class clubMember:
     def __init__(self,name):
          New_clubmember = Person(name)

Now, I believe this code is correct because it doesn't raise any error at runtime. However, I can't write any code that will you the object "New_clubmember.".

Any other function which is written to use New_clubmember says that it has not been defined.

My question is: How do I use the New_clubmember object --which was created by the __init__ statement in club_Member -- in a function which is outside the clubMember class?

Recommended Answers

All 13 Replies

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

class clubMember:
     def __init__(self,name):
          New_clubmember = Person(name)

apologies.. the code example should look like this.. I couldn't find the edit button for some reason so I posted this.

I think you are talking about something like this, although it seems a round-about way of doing it.

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

CM = ClubMember("Harry")
print CM.new_clubmember.name

thanks for your reply.. but that's not what I want.
I just want to create a normal instance of the Person class using the __init__ of the ClubMember class. The Person object has to be accessible to the whole program.

#here's what I'm trying to do in code
>>> class Person:
	def __init__(self,name):
		self.name = name

>>> class clubmember:
	def __init__(self,name):
	#create and instance of the person class called new_member.	
        new_member = Person(name) 

>>> xx = clubmember("martin")
>>> new_member.name
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    new_member.name
NameError: name 'new_member' is not defined
>>>

Now why does calling the new_member instance raise an error?

Now why does calling the new_member instance raise an error?

Look at the indentations. Generally, that kind of thing happens when you use tabs instead of spaces, which is why spaces is the preferred way to go.

The Person object has to be accessible to the whole program

It's the same, use "self". You are perhaps trying to make this harder than it is.

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

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

         print "testing print_name in class =",
         self.print_name()

         self.change_name("no longer " + self.new_clubmember.name)
         self.print_name()

     def change_name(self, next_name):
         self.new_clubmember.name = next_name

     def print_name(self):
         print self.new_clubmember.name

CM = ClubMember("Harry")
print
CM = ClubMember("Hands")

Doesn't that make the Person object an attribute of the clubmember class? Is that appropriate?
I'd have to access it by saying
CM.new_clubmember.name

Doesn't that make the Person object an attribute of the clubmember class

I thought that was what you wanted. You can define the Person prototype and use it in several ClubMember class instances for several different people. You could also use a text file or an SQL file to store the data and access it from the file. A comma delimited text file would just be
Name=Harry, Score=12
Name=Joe, Score=1
which you would parse to find the name, etc. Otherwise, use a class and pickle to save the results.

I'd have to access it by saying
CM.new_clubmember.name

Yes. You can also use inheritance so the Person class becomes a child of the ClubMember class, and ClubMember would have access to all of its variables and methods.

well.. isn't there a simple way? I mean, it sounds a bit tedious (not to mention difficult) to use SQL files and the like.. (I'm new to programming).
Isn't there a way to just create a instance of the Person class (which is independent of any other class) by using the __init__ statement of the submember class?

(It is possible to add the
new_person = Person(name)
line into the __init__ statement of the club_member class but then when I type
new_member.name
I get the error I posted previously.)

Futhermore, thanks for your suggestion on using a child class, but I don't think that would work for me.

To make life easier you could use a helper method ...

# creating a class instance within another class

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

class ClubMember:
    def __init__(self,name):
        self.new_clubmember = Person(name)
    
    def expose(self):
        return self.new_clubmember.name


bob = ClubMember('Bob Sponge')
mary = ClubMember('Mary Keester')

# access info this way ...
print(bob.new_clubmember.name)  # Bob Sponge

# or use helper method 'expose' ...
print(bob.expose())   # Bob Sponge
print(mary.expose())  # Mary Keester

thanks ... I'll try that and post back.

Could I use this "helper" method to return the Person object itself? (and not just the name)

Try it, you might like it!

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.