# Python user-defined class question

**URL:** <https://boards.straightdope.com/t/python-user-defined-class-question/431097>\
**Category:** Factual Questions\
**Created:** [December 24, 2007, 4:42am UTC](https://boards.straightdope.com/t/python-user-defined-class-question/431097 "2007-12-24T04:42:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hostile\_Dialect](https://avatars.discourse-cdn.com/v4/letter/h/e495f1/32.png) [@Hostile\_Dialect](https://boards.straightdope.com/u/Hostile_Dialect)\
**Post date:** [December 24, 2007, 4:42am UTC](https://boards.straightdope.com/t/python-user-defined-class-question/431097/1 "2007-12-24T04:42:04Z")

</div>

I recently picked up a copy of O’Reilly’s Learning Python, 3rd ed., which was published all of two months ago. I like it so far, but when I got to the part about user-defined classes and put this block of code into IDLE:

```auto

class Worker:
    def __init__ (self, name, pay):
        self.name = name
        self.pay = pay
        def lastName(self):
            return self.name.split()[-1]
        def giveRaise(self, percent):
            self.pay *= (1.0 + percent)

```

And then defined my workers, Jim and Sue:

```auto

jim = Worker('Jim Smith', 50000)
sue = Worker('Sue Jones', 60000)

```

My “name” and “pay” calls seemed to work:

```auto

>>> jim.name
'Jim Smith'
>>> sue.pay
60000

```

But then I tried to find Jim’s last name and give Sue a raise and got this:

```auto

>>> jim.lastName()

Traceback (most recent call last):
  File "<pyshell#57>", line 1, in <module>
    jim.lastName()
AttributeError: Worker instance has no attribute 'lastName'

>>> sue.giveRaise(.10)

Traceback (most recent call last):
  File "<pyshell#76>", line 1, in <module>
    sue.giveRaise(.10)
AttributeError: Worker instance has no attribute 'giveRaise'

```

In fact, running the same thing through PyAlaMode indicates that the only attributes available for jim and sue are: name, pay, **class** , **dict** , **doc** , **init** and **module**.

What’s the deal? Can anyone help me figure out what the problem is here?

---

<div class="post-metadata">

**Author:** ![Walton\_Firm](https://avatars.discourse-cdn.com/v4/letter/w/45deac/32.png) [@Walton\_Firm](https://boards.straightdope.com/u/Walton_Firm)\
**Post date:** [December 24, 2007, 10:33am UTC](https://boards.straightdope.com/t/python-user-defined-class-question/431097/2 "2007-12-24T10:33:52Z")

</div>

It’s an indentation problem. You are defining lastName and giveRaise inside the definition of **init** , therefore they are local functions rather than class members, and will not be visible outside of **init**. To make them methods of the class, indent them so that they are at the same level as **init**.

---

<div class="post-metadata">

**Author:** ![Hostile\_Dialect](https://avatars.discourse-cdn.com/v4/letter/h/e495f1/32.png) [@Hostile\_Dialect](https://boards.straightdope.com/u/Hostile_Dialect)\
**Post date:** [December 24, 2007, 10:39am UTC](https://boards.straightdope.com/t/python-user-defined-class-question/431097/3 "2007-12-24T10:39:31Z")

</div>

Thanks. I’ve tried that, too, though; same problem. I also tried them inside **init** and then tried to call something like joe. **init**.lastName() (just for shits and giggles) and that didn’t work.

(I love the SDMB. You post a Python question and the first person to reply is Dutch! 🆒 )

---

<div class="post-metadata">

**Author:** ![Bytegeist](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bytegeist](https://boards.straightdope.com/u/Bytegeist)\
**Post date:** [December 24, 2007, 1:53pm UTC](https://boards.straightdope.com/t/python-user-defined-class-question/431097/4 "2007-12-24T13:53:41Z")

</div>

Your code works fine over here, after the indentation is fixed. I agree with **Walton** that it’s likely an identation problem — still.

It’s possible that you’ve got both tabs and spaces in your indents, and that this is confusing the Python parser. Something worth checking anyway.

---

<div class="post-metadata">

**Author:** ![Hostile\_Dialect](https://avatars.discourse-cdn.com/v4/letter/h/e495f1/32.png) [@Hostile\_Dialect](https://boards.straightdope.com/u/Hostile_Dialect)\
**Post date:** [December 24, 2007, 8:16pm UTC](https://boards.straightdope.com/t/python-user-defined-class-question/431097/5 "2007-12-24T20:16:32Z")

</div>

Hrm. I’m pretty sure I didn’t, but I’ll try it again anyway.

If that doesn’t work, maybe I’ll uninstall and reinstall Python, or something; if the code works over there and doesn’t work over here, there’s obviously a problem with my implementation somehow.

…Well, I’ll be damned. After a reboot, it works fine in IDLE (with the indentation fixed).

---

<div class="post-metadata">

**Author:** ![Elendil\_s\_Heir](https://avatars.discourse-cdn.com/v4/letter/e/7cd45c/32.png) [@Elendil\_s\_Heir](https://boards.straightdope.com/u/Elendil_s_Heir)\
**Post date:** [December 24, 2007, 9:09pm UTC](https://boards.straightdope.com/t/python-user-defined-class-question/431097/6 "2007-12-24T21:09:29Z")

</div>

If you recognize the name “Raymond Luxury Yacht,” you are a member of the Python class.
