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 8688743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:27:12+00:00 2026-06-12T23:27:12+00:00

Here is a code snippet from Zed Shaw’s Learn Python the Hard Way tutorial

  • 0

Here is a code snippet from Zed Shaw’s “Learn Python the Hard Way” tutorial 40:

class Song(object):

    def __init__(self, lyrics):
        self.lyrics = lyrics

    def sing_me_a_song(self):
        for line in self.lyrics:
            print line

Why does Python allow “self.lyrics” to be used when defining “sing_me_a_song” function? Is it because whatever variable
is defined under “init” can also be used elsewhere in the same class?

Thanks.

  • 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-12T23:27:13+00:00Added an answer on June 12, 2026 at 11:27 pm

    Instance Variables

    This is called an instance variable. Any variable defined with self. as a “prefix” can be used in any method of an object. Typically such variables are created in __init__, so they can be accessed from the moment the object is initialized, though you can define instance variables in other methods. For example:

    >>> class foo:
    ...     def fun(self):
    ...             self.heh=3
    ... 
    >>> f = foo()
    >>> f.heh 
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: foo instance has no attribute 'heh'
    >>> f.fun()
    >>> f.heh
    3
    

    Notice the danger in initializing instance variables outside of __init__; if the method in which they are initialized is not called before you try to access them, you get an error.

    Class Variables

    This is not to be confused with “class variables,” which are another type of variable that can be accessed by any method of a class. Class variables can be set for an entire class, rather than just specific objects of that class. Here’s an example class with both instance and class variables to show the difference:

    class MyClass:
        classVar = "I'm a class var."
        def __init__(self):
            self.instanceVar = "I'm an instance var."
        def fun(self):
            methodVar = "I'm a method var; I cannot be accessed outside of this method."
            self.instanceVar2 = "I'm another instance var, set outside of __init__."
    

    A Note on “methods” vs “functions”

    In your question, you call sing_me_a_song a “function.” In reality, it is a method of the class Song. This is different from a regular old function, because it is fundamentally linked up with the class, and thus objects of that class as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

FULL CLASS CODE HERE: http://pastebin.com/rdjDGLJS EDIT: Latest code snippet taken from original posters comment
Here is my code snippet where I'am taking confidence with initialization blocks class Father{
I have a class with two constructors (C#). Here is the code snippet: public
Here is a code snippet from DirectShowNet library, used to open a video file:
Code snippet from here : void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char
Code snippet from here : void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char
Here is a code snippet from my project: template<typename Second, typename First = const
Here's an ASPX code snippet from when I was trying to get multiple values
Here is a code snippet from Network Programming for Microsoft Windows : ... //
Here's a code snippet from OpenJDK6's hotspot/src/share/vm/prims/unsafe.cpp (starting on line 1082): // JSR166 ------------------------------------------------------------------

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.