Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2.6: Class inside a Class?

Tags:

python

class

Hey everyone, my problem is that im trying to figure out how to get a class INSIDE another class.

What I am doing is I have a class for an Airplane with all its statistics as to how fast it can fly, how far it can go, fuel consumption, and so on. Then I have a Flight Class which is all the details about the flight: Distance, starting location and time, ending location and time, duration, and so on.

But I realized that each airplane has multiple flights, so why not put all the flight data into the airplane class? Although how do i put a class INTO another class so i can call something like this:

Player1.Airplane5.Flight6.duration = 5 hours 

Ive somewhat done it with the airplane class, but when i go to save the information (listing everything out into a text document), all it gives me is the Hex location of the data and not the actual strings.

class Player (object):#Player Class to define variables     '''Player class to define variables'''      def __init__ (self, stock = 0, bank = 200000, fuel = 0, total_flights = 0, total_pax = 0):         self.stock = stock         self.bank = bank         self.fuel = fuel         self.total_flights = total_flights         self.total_pax = total_pax         self.Airplanes = Airplanes         self.flight_list = flight_list 

Is there a way to put a class inside a class? or will i need to make one super Player class which handles all the information which im using other classes for?

like image 589
Pilot824 Avatar asked Jul 11 '10 07:07

Pilot824


People also ask

Can you have a class inside a class Python?

You can have more than one inner class in a class. As we defined earlier, it's easy to implement multiple inner classes. class Outer: """Outer Class""" def __init__(self): ## Instantiating the 'Inner' class self.

Can you call a class inside another class Python?

Call method from another class in a different class in Python. we can call the method of another class by using their class name and function with dot operator. then we can call method_A from class B by following way: class A: method_A(self): {} class B: method_B(self): A.

Can you nest classes in Python?

One of the most useful and powerful features of Python is its ability to nest classes within classes. A nested class is a class defined within another class, and inherits all the variables and methods of the parent class.

Can we create objects inside class in Python?

Creating an Object in Python It can also be used to create new object instances (instantiation) of that class. The procedure to create an object is similar to a function call. This will create a new object instance named harry . We can access the attributes of objects using the object name prefix.


2 Answers

I think you are confusing objects and classes. A class inside a class looks like this:

class Foo(object):     class Bar(object):         pass  >>> foo = Foo() >>> bar = Foo.Bar() 

But it doesn't look to me like that's what you want. Perhaps you are after a simple containment hierarchy:

class Player(object):     def __init__(self, ... airplanes ...) # airplanes is a list of Airplane objects         ...         self.airplanes = airplanes         ...  class Airplane(object):     def __init__(self, ... flights ...) # flights is a list of Flight objects         ...         self.flights = flights         ...  class Flight(object):     def __init__(self, ... duration ...)         ...         self.duration = duration         ... 

Then you can build and use the objects thus:

player = Player(...[     Airplane(... [         Flight(...duration=10...),         Flight(...duration=15...),         ] ... ),     Airplane(...[         Flight(...duration=20...),         Flight(...duration=11...),         Flight(...duration=25...),         ]...),     ])  player.airplanes[5].flights[6].duration = 5 
like image 99
Marcelo Cantos Avatar answered Sep 22 '22 14:09

Marcelo Cantos


It sounds like you are talking about aggregation. Each instance of your player class can contain zero or more instances of Airplane, which, in turn, can contain zero or more instances of Flight. You can implement this in Python using the built-in list type to save you naming variables with numbers.

class Flight(object):      def __init__(self, duration):         self.duration = duration   class Airplane(object):      def __init__(self):         self.flights = []      def add_flight(self, duration):         self.flights.append(Flight(duration))   class Player(object):      def __init__ (self, stock = 0, bank = 200000, fuel = 0, total_pax = 0):         self.stock = stock         self.bank = bank         self.fuel = fuel         self.total_pax = total_pax         self.airplanes = []       def add_planes(self):         self.airplanes.append(Airplane())    if __name__ == '__main__':     player = Player()     player.add_planes()     player.airplanes[0].add_flight(5) 
like image 45
Johnsyweb Avatar answered Sep 26 '22 14:09

Johnsyweb