0

Sorry for the confusing title because I really don't know how to describe this question. I will try to use an example to explain.

Say I want to write a class

class Line:
    def __init__(self, x1, y1, x2, x3):
        self.start = (x1, y1)
        self.end = (x2, y2)
    def length(self, metric):
        # return the length of this line using the input metric

Here metric is a metric on the plane (it might be a function, or a table etc, not important here)

Now I want to do something like

def findLine(metric):
    l1 = Line(0,0,1,1)
    l2 = Line(0,0,2,2)
    # just an example, I may need to create a lot of lines then compare their length
    if l1.length(metric)>l2.length(metric):
        return l1

I am looking for a way that somehow setting a default value for metric for all the lines used in findLine

so I can simply call l1.length()>l2.length() in findLine.

Also, the data metric might be stored in a large data frame. I think it might be not good to store them in each line.

Sorry for the confusing. I am just trying to find a way to simplify my code.


I should add that in my code, there are 5 or 6 these kind of parameters not just one.

That's the reason I want to find a way to not writing all parameters every time.

Thanks!

4
  • You can pass ‘metric’ into each Line you create. Commented Aug 21, 2020 at 8:10
  • 1
    The question is confusing as well as the title. Are you looking for a default attribute value, e.g. def length(self, metric='xxx'):? Commented Aug 21, 2020 at 8:10
  • I have edited the question, hope it is clearer. Thank you. Commented Aug 21, 2020 at 8:43
  • You say that: data metric might be stored in a large data frame, but if every Line has its own reference to it, you only increase Line by an additional reference, and not by the size of the large data frame. Commented Aug 21, 2020 at 8:56

1 Answer 1

1

You could use a class attribute:

class Line:
    metric = (1,2)

    def __init__(self, x1, y1, x2, y2):
        self.start = (x1, y1)
        self.end = (x2, y2)

    def length(self):

        return Line.metric+self.start
# return the length of this line using the input metric

l = Line(1,2,3,4)
m = Line(4,5,6,7)

print(l.metric)
print(m.metric)
print(l.length())
print(m.length())

This belongs to the class and not the instance. Any instance will have the same value for metric. You can access the value for metric within the instance by calling the original Line class as opposed to self.

If you want metric to occur only in some instances but not others, you had better add it as an instance attribute:

class Line:
    def __init__(self, x1, y1, x2, y2, metric=None):
        self.start = (x1, y1)
        self.end = (x2, y2)
        self.metric = metric

    def length(self):
        if self.metric is not None:
            return self.metric + self.start
        else:
            return 'Line has no metric'
    # return the length of this line using the input metric


metric1 = (1,2)
metric2 = (2,3)
l = Line(1, 2, 3, 4, metric=metric1)
m = Line(4, 5, 6, 7, metric=metric2)
n = Line(8, 9, 10, 11)

print(l.metric)
print(m.metric)
print(l.length())
print(m.length())
print(n.length())
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you! Do you think it is possible to do this without storing the metric into every lines?
Yes, but then the lines which don't have a metric would not be able to use the length method. Are you OK with that?
This is just what @user32882 has done: There is one metric, but every instance can see that one metric.
I thought by doing this, it would copy a new metric and store it into each line. But it actually will only reference to the origin metric, is the right? Thanks.
Why don't you pass metric into your __init__ method then? That will make it an instance attribute. I'll edit my answer to show what I mean
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.