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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:39:47+00:00 2026-06-17T21:39:47+00:00

I’m still getting my head around classes in Python, class Triangle(object): def __init__(self, angle1,

  • 0

I’m still getting my head around classes in Python,

class Triangle(object):
    def __init__(self, angle1, angle2, angle3):
        angle1 = self.angle1
        angle2 = self.angle2
        angle3 = self.angle3
    number_of_sides = 3
    def check_angles(angle1, angle2, angle3):
        if angle1+angle2+angle3 == 180:
            return True
        else:
            return False

my_triangle = Triangle(30, 60, 90)
print(Triangle.number_of_sides)
print(Triangle.check_angles)

So what’s wrong?

Stackoverflow has some really mean users, I come from MathStackExchange and people didn’t downvote novice/noob questions as much as they do here.

  • 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-17T21:39:48+00:00Added an answer on June 17, 2026 at 9:39 pm

    You have your assignments backward:

    self.angle1 = angle1
    

    etc.

    Thinking about what is going on a little bit might help here. self is an instance of the class Triangle that is passed in. So,

    T = Triangle()
    T.method_call()  #basically the same as `Triangle.method_call(T)`
    

    Within method_call, self is a reference to T. (__init__ is kind of magic — It gets called automatically on the first line, but the principle is still the same). Once you know that, it’s easy to see why you need self.whatever = whatever — You’re putting a new attribute on the object T!
    Finally, this explains how you should write check_angles:

    def check_angles(self):
        #remember, `self` is a reference to a triangle instance -- T in our hypothetical code
        #so we can use `self` to gain access to all the attributes we set in `__init__`.
        if self.angle1 + self.angle2 + self.angle3 == 180:
            return True
        else:
            return False
    

    Now a quick diversion into class attributes. You can put attributes onto a class as well:

    class Triangle:
        number_of_sides = 3
    

    I think it is customary to define class attributes before you define your methods (functions), but you don’t actually have to. (It will help the readers of your code understand it though — I misunderstood your original code because of the order you did things).

    You can dynamically add attributes to a class after the class has been created as well:

    class Triangle:
        ...
    
    Triangle.number_of_angles = Rhombus.number_of_angles - 1
    

    You can get access to Triangle.some_attribute a few ways. The first way is directly through the class (Triangle.number_of_sides). The second way is via an instance:

    T = Triangle()
    T.number_of_sides
    

    This seems a little funny at first. After all, the instance doesn’t have a number_of_sides attribute. It turns out that python is designed to look at the instance first to see if it has it’s own number_of_sides attribute. If it does, great, that’s what you get. If it doesn’t however, python will then look at the class for the attribute. This turns into a handy way to share data between instances of a class.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
i got an object with contents of html markup in it, for example: string
I have a view passing on information from a database: def serve_article(request, id): served_article
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.