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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:31:57+00:00 2026-06-07T21:31:57+00:00

Say there is a class: class x(obj): y = 1 What is faster (or

  • 0

Say there is a class:

class x(obj):
    y = 1

What is faster (or preferred):

    def __init__(self):
        print self.y

or:

    def __init__(self):
        print x.y

I assume x.y better communicates the intend but I’m interested in the speed implications.

  • 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-07T21:31:59+00:00Added an answer on June 7, 2026 at 9:31 pm

    The performance gain you could possibly achieve with these micro optimizations doesn’t matter. The impact of the printing dwarfs the cost of attribute access by far. For reference, here’s a test script:

    import sys,timeit
    class ClassAccess(object):
        y = 1
        def __init__(self):
            print(ClassAccess.y)
    
    class SelfAccess(object):
        y = 1
        def __init__(self):
            print(self.y)
    
    ca = timeit.timeit(ClassAccess, number=100000)
    sa = timeit.timeit(SelfAccess, number=100000)
    
    sys.stderr.write(str(ca) + "\n")
    sys.stderr.write(str(sa) + "\n")
    

    On my machine (with the yakuake terminal), this outputs

    0.640013933182
    0.628859043121
    

    This is within experimental error of both variants being identical. Crude experimentation shows that:

    • Approximately 90% of the runtime is caused by actually displaying the printed result.
    • Of the rest, approximately 50% is the time that the print statement alone takes up.
    • Approximately 80% of the rest of that is caused by the allocation of the objects.

    Therefore, it’s safe to say to derive the conclusion that there is no difference in performance.

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

Sidebar

Related Questions

Lets say there is setup like this: public class MyClass { public void DoSomething(string
In my program, there are two class, say, category and product, with each object
Say, there is a following example: class Super { public int i = 3;
Is there a clean method of mocking a class with generic parameters? Say I
Say you have a std::list of some class. There are two ways you can
In VB.NET there is a keyword 'shadows'. Let's say I have a base class
Let's say that I have this: class Dependency1 { def methodD1 { } }
let's suppose there are two objects of class abc abc obj = new abc(10,20);
Let's say I have these models: class A(models.Model): name = models.CharField(max_length=32) b = models.ForeignKey(B,
Let's say we have these two classes: public class Base { public static int

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.