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()
1
  • 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?! Commented Aug 6, 2014 at 11:19

5 Answers 5

11

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, can you tell me what def __str__ does?
Sure. This is a special method which provides a string representation of object. See: docs.python.org/2/reference/datamodel.html#object.__str__
Do we need the "object" in the class Order(object) line? What does it do?
Note: setting the object subclass is unnecessary in Python > 3.x, see: stackoverflow.com/questions/2588628/…
@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 prints should function in Python 3. Though this is out of the scope of the question's purpose.
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"

1 Comment

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.
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"

Comments

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

1 Comment

I'm not sure a restaurant that can only handle one order would stay in business long...
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.