1

Can anyone describe how to return objects from a method on which further methods and attributes can be accessed?

Example:

pizza = PizzaHut()
order = pizza.order()
print order.order_number
order.cancel()
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Delete Me
  • 111
  • 1
  • 4
  • 12
  • 2
    **Every** function or method call returns an object (even just `None`, in the trivial case) whose own attributes (including methods) can then be accessed. What exactly is your question?! – jonrsharpe Aug 06 '14 at 11:19

5 Answers5

7

Create an Order class with appropriate methods and properties. After that, you'll be able to return an instance of this class from PizzaHut.order() method.

class Order(object):
    def __init__(self, number, amount):
        self.number = number
        self.amount = amount
        print self
    
    def __str__(self):
        return "Order #%s: amount = %s" % (self.number, self.amount)
        
    @property
    def order_number(self):
        return self.number

    def cancel(self):
        self.amount = 0
        print "Order is cancelled."
        print self
        

class PizzaHut(object):
    
    def __init__(self, price):
        self.price = price

    def order(self):
        return Order(42, self.price)

pizza = PizzaHut(4.99)
order = pizza.order()
print order.order_number
order.cancel()

http://repl.it/WWB


Python 3 version

class Order:
    def __init__(self, number, amount):
        self.number = number
        self.amount = amount
        print(self)
    
    def __str__(self):
        return f'Order #{self.number}: amount = {self.amount}'
        
    @property
    def order_number(self):
        return self.number

    def cancel(self):
        self.amount = 0
        print('Order is cancelled.')
        print(self)
        

class PizzaHut:
    def __init__(self, price):
        self.price = price

    def order(self):
        return Order(42, self.price)

pizza = PizzaHut(4.99)
order = pizza.order()
print(order.order_number)
order.cancel()

Py3 repl: https://replit.com/@f0t0n/so-25158930#main.py

Eugene Naydenov
  • 7,165
  • 2
  • 25
  • 43
  • Thanks, can you tell me what `def __str__` does? – Delete Me Aug 06 '14 at 17:15
  • Sure. This is a special method which provides a string representation of object. See: https://docs.python.org/2/reference/datamodel.html#object.__str__ – Eugene Naydenov Aug 06 '14 at 17:25
  • Do we need the "object" in the class Order(object) line? What does it do? – Caleb Apr 01 '22 at 01:52
  • 1
    Note: setting the object subclass is unnecessary in Python > 3.x, see: https://stackoverflow.com/questions/2588628/what-is-the-purpose-of-subclassing-the-class-object-in-python – Caleb Apr 01 '22 at 01:54
  • @Caleb You're right. Also nowadays I would better use string interpolation in the `__str__` instead of old string formatting using `%`. :) And also all the `print`s should function in Python 3. Though this is out of the scope of the question's purpose. – Eugene Naydenov Apr 11 '22 at 09:45
1

you can use Namedtuplesin in python.

# Using namedtuple is way shorter than
# defining a class manually:
>>> from collections import namedtuple
>>> Car = namedtuple('Car', 'color mileage')

# Our new "Car" class works as expected:
>>> my_car = Car('red', 3812.4)
>>> my_car.color
'red'
>>> my_car.mileage
3812.4

# We get a nice string repr for free:
>>> my_car
Car(color='red' , mileage=3812.4)

# Like tuples, namedtuples are immutable:
>>> my_car.color = 'blue'
AttributeError: "can't set attribute"
Amin
  • 11
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 20 '22 at 06:49
0
class Foo():
    def f(self):
        print "Foo.f called"

class Bar():
    def b(self):
        return Foo()

bar = Bar()
bar.b().f() # prints "Foo.f called"
0
class Order:
   def __init__(self, number):
      self.order_number = number


class PizzaHut:
   def __init__(self):
      self.order = Order(1234)

   def order(self):
      return self.order
Miguel Prz
  • 13,718
  • 29
  • 42
0

This is how to return back the object from a method

class Order(object):
    def __init__(self, number, amount):
       self.number = number
       self.amount = amount

    def cancel(self):
       self.amount = 0
       return self  #here you return back the object