Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Python creating objects

The essential properties of a Thing are as follows :

  1. The constructor should take in 1 parameter, the name of the Thing.

stone = Thing ('stone ')

  1. owner : an attribute that stores the owner object of the Thing, usually a Person object.

In OOP, we set this attribute to None during initialization when this Thing does not belong to any Person yet (to signify the absence of an object value).

stone . owner

None

  1. is_owned(): returns a boolean value, True if the thing is "owned" and False otherwise.

stone . is_owned ()

False

4.get_owner(): returns the Person object who owns the Thing object.

stone . get_owner ()

None

Implement the class Thing such that it satisfies the above properties and methods.

im not sure what is wrong with my code:

class Thing:
def __init__(self,name):
 self.name=name
 self.owner=None
def is_owned(self):
 return self.owner!=None
def get_owner(self):
 return self.owner

My question: as the question states, when i input stone.owner, i expect to receive an output None. however, there is no output at all. edit: no output received is accepted instead of None. However, is there any way to return None from stone.owner?

Answer*

Draft saved
Draft discarded
Cancel
2
  • 1
    A bit pedantic but - self as an argument is just a convention it could be called anything. Instance methods receive the instance as a the first parameter (usually called self), classmethods receive the class as the first parameter (usually called cls) and staticmethods receive nothing. Commented Apr 9, 2017 at 7:13
  • Thank you, @AChampion ! I didn't know that! Commented Apr 9, 2017 at 7:14

lang-py

AltStyle によって変換されたページ (->オリジナル) /