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

  • Home
  • SEARCH
  • 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 8398901
In Process

The Archive Base Latest Questions

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

Can you please clarify about the perimeter variable in the below class. I understand

  • 0

Can you please clarify about the perimeter variable in the below class.

I understand that the self.vertices is to a particular instance. Since perimeter is not defined with self, does that mean its a class variable here? Then is it not common to all the instances?

Is it not the right way to code the perimeter as self.perimeter, so its aptly declared to each instance?

This code is from a book.

Polygon.py

import math
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def distance(self, p2):
        return math.sqrt((self.x-p2.x)**2 + (self.y-p2.y)**2)

class Polygon:
    def __init__(self):
        self.vertices = []
    def add_point(self, point):
        self.vertices.append((point))
    def perimeter(self):
        perimeter = 0
        points = self.vertices + [self.vertices[0]]
        for i in range(len(self.vertices)):
            perimeter += points[i].distance(points[i+1])
        return perimeter
>>> square = Polygon() 
>>> square.add_point(Point(1,1)) 
>>> square.add_point(Point(1,2)) 
>>> square.add_point(Point(2,2)) 
>>> square.add_point(Point(2,1)) 
>>> square.perimeter() 
4.0 

New type

import math
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def distance(self, p2):
        return math.sqrt((self.x-p2.x)**2 + (self.y-p2.y)**2)

class Polygon:
        def __init__(self):
            self.vertices = []
        def add_point(self, point):
            self.vertices.append((point))
        def perimetermethod(self):
            self.perimeter = 0
            points = self.vertices + [self.vertices[0]]
            for i in range(len(self.vertices)):
                self.perimeter += points[i].distance(points[i+1])
            return self.perimeter

if __name__=='__main__':
    p1 = Polygon()
    p1.add_point(Point(1,1))
    p1.add_point(Point(1,2))
    p1.add_point(Point(2,2))
    p1.add_point(Point(2,1))
    print(p1.perimetermethod())
  • 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-09T21:09:48+00:00Added an answer on June 9, 2026 at 9:09 pm

    Assigning a new variable with some_name = ... always creates the variable in the innermost enclosing scope (unless global or nonlocal are in play, but they’re not relevant here). Assigning a new attribute name on an object creates the attribute on that object.

    So self.foo = 1 assigns an attribute named foo on the object currently referred to by self. Conventionally, the name self is used as the first parameter to a method, which receives the object on which the method was invoked. So “defining a variable with self” isn’t anything special; it’s just the ordinary rules about assigning to attributes on existing object. Any attributes that exist in instance object itself obviously have to be specific to that instance.

    perimeter = 0 inside the perimeter method of the Polygon class creates a variable in the innermost enclosing scope. That’s the perimeter method, so it creates a local variable. A local variable only exists for the duration of the function call, so it’s neither a class variable nor an instance variable. You can’t access it from anywhere except within the scope of that particular method (and it has a new completely independent value on each invocation), so it can’t be common to all the instances. But neither can you access a different value for it on each particular instance, so it’s not an instance variable either.

    If you had perimeter = 0 outside a method, in the class block itself, then the innermost enclosing scope would be the class block. That would create a “class variable”, which is just an attribute on the class object. If an attribute is on a class, then obviously it can’t be specific to any instance, because there’s only one class but there can be any number of instances. As an aside, this is exactly what the __init__, add_point, and perimeter methods of the Polygon class are; they were assigned (with a def statement) in a class block, so they became attributes of the class object.


    Summary:

    1. self.foo = 1 is assigning to an attribute on the object currently referenced by self (this is usually the “current instance”)
    2. foo = 1 in a class block is creating a class attribute of the class being defined
    3. foo = 1 in a def block is creating a local variable of the function being defined

    But you shouldn’t really memorise it that way. They’re just special cases of:

    1. Assigning to a dotted name like foo.bar.baz = 1 is writing to an attribute of an object
    2. Assigning to a simple name like foo = 1 is writing to a variable in the innermost enclosing scope
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm kinda of new to PHP, can you please clarify about the below preg_match.
Can someone please clarify using a SIMPLE user story the full slice of what
Can someone please help to clarify? Also, please mention if there are other representation
Can you clarify this please? public static void MyMethod() { var context= new MyModel.Entities();
Can someone please tell me what this means: 07-04 09:54:38.048: I/DetailActivity(15496): Title that is
Before anything, let me first clarify that the below thoughts are purely my personal
I was wondering can you please let me know about the following two scenarios
Can please someone explain to me or bring me on the way how to
Can please some tell me when does the above written error occurs. I just
Someone can please answer to me, if have any Editor or Compiler to C

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.