Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8332295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:40:50+00:00 2026-06-09T02:40:50+00:00

I know a ton has been written on this subject. I cannot, however, absorb

  • 0

I know a ton has been written on this subject. I cannot, however, absorb much of it. Perhaps because I’m a complete novice teaching myself without the benefit of any training in computer science. Regardless, maybe if some of you big brains chime in on this specific example, you’ll help other beginners like me.

So, I’ve written the following function which works just fine when I call it (as a module?) as it’s own file called ‘funky.py’:

I type the following into my terminal:

python classy.py

and it runs fine.

def load_deck():
    suite = ('Spades', 'Hearts')
    rank = ('2', '3')
    full_deck = {}
    i = 0
    for s in suite:
        for r in rank:
            full_deck[i] = "%s of %s" % (r, s)
            i += 1
    return full_deck

print load_deck()

When I put the same function in a class, however, I get an error.

Here’s my code for ‘classy.py’:

class GAME():
    def load_deck():
        suite = ('Spades', 'Hearts')
        rank = ('2', '3')
        full_deck = {}
        i = 0
        for s in suite:
            for r in rank:
                full_deck[i] = "%s of %s" % (r, s)
                i += 1
        return full_deck
MyGame = GAME()
print MyGame.load_deck()

I get the following error:

Traceback (most recent call last):
File "classy.py", line 15, in <module>
print MyGame.load_deck()
TypeError: load_deck() takes no arguments (1 given)

So, I changed the definition line to the following and it works fine:

def load_deck(self):

What is it about putting a function in a class that demands the use of ‘self’. I understand that ‘self’ is just a convention. So, why is any argument needed at all? Do functions behave differently when they are called from within a class?

Also, and this is almost more important, why does my class work without the benefit of using init ? What would using init do for my class?

Basically, if someone has the time to explain this to me like i’m a 6 year-old, it would help. Thanks in advance for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:40:51+00:00Added an answer on June 9, 2026 at 2:40 am

    Defining a function in a class definition invokes some magic that turns it into a method descriptor. When you access foo.method it will automatically create a bound method and pass the object instance as the first parameter. You can avoid this by using the @staticmethod decorator.

    __init__ is simply a method called when your class is created to do optional setup. __new__ is what actually creates the object.

    Here are some examples

    >>> class Foo(object):
        def bar(*args, **kwargs):
            print args, kwargs
    
    >>> foo = Foo()
    >>> foo.bar
    <bound method Foo.bar of <__main__.Foo object at 0x01C9FEB0>>
    >>> Foo.bar
    <unbound method Foo.bar>
    >>> foo.bar()
    (<__main__.Foo object at 0x01C9FEB0>,) {}
    >>> Foo.bar()
    
    Traceback (most recent call last):
      File "<pyshell#29>", line 1, in <module>
        Foo.bar()
    TypeError: unbound method bar() must be called with Foo instance as first argument (got nothing instead)
    >>> Foo.bar(foo)
    (<__main__.Foo object at 0x01C9FEB0>,) {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this sort of question has been asked here before, but still
I am sure this has been asked a ton of times, but would appreciate
I know this topic has been covered ad nauseam here, and other places on
I know something like this has been asked before, but the answer was sort
Ok, before I get a ton of responses. I know you cannot talk to
Know this might be rather basic, but I been trying to figure out how
I know it has been asked many times... but I can't seem to figure
Apologies if this has already been covered or you think it really belongs on
I know there is a ton of stuff on this already and have tried
I'm almost sure this question has been asked before, but for the love of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.