Not sure how to quite phrase it, but here's the deal. I have a script where every variable is defined globally, but later I want to convert the script into a class for better encapsulation.
However, all the variables I used in the script needs to be converted into object variables. I would have changed something like
x = 5
into
self.x = 5
And after doing that all the functions in the script needs to be turned into methods of the class, however, most of the methods are mathematical formulas, and refactoring something as clean as
z = x ** y + x
into
z = self.x ** self.y + self.x
really hurts readability.
So as a solution to this I've been typing these really awkward re-naming at the beginning of the methods:
def method(self, ...):
x = self.x
y = self.y
...
It makes the formulas readable but typing all that re-naming is really painful, is there a more elegant way of doing this?
self is only a suggested name. I would encourage you to use it, but in some cases it is better to shorten it.
class A:
x = None
y = None
def calculate_z(s):
return s.x ** s.y + s.x
foo = A()
foo.x = 4
foo.y = 2
foo.calculate_z() # 20
Fun fact: you could even use unicode characters, like ® (in Python 3, that is).
def calculate_z(®):
return ®.x ** ®.y + ®.x
Well, there could be some other solutions, but they're all ugly hacks at most.
You can make the code possibly less awkward by using multiple assignment though:
x, y = self.x, self.y
In addition x and y are faster to use in CPython than self.x and self.y, because the latter ones would need a costly attribute lookup for every use.
Related
I am trying to understand what is the difference between creating a method inside a class and creating a function. Both gives same output. Pls provide clarity
class Point:
""" Create a new Point, at coordinates x, y """
def __init__(self, x=0, y=0, z=0):
""" Create a new point at x, y """
self.x = x
self.y = y
self.z = ((self.x ** 2) + (self.y ** 2)) ** 0.5
and this function:
def distance(x,y):
z=((x ** 2) + (y ** 2)) ** 0.5
return z
both looks same to me
p = Point(3,4)
p.x, p.y, p.z
output:
(3, 4, 5.0)
and
distance(3,4)
output: 5.0
The difference is the self reference - the class instance has access to its own local variables and its methods. In this small example there isn't any need for a class. The function returns a tuple object much like the Point object you created. There is a rule: If a class has two methods and one of them is __init__, you shouldn't have a class. In your case, you didn't even reach the two method bar. (That rule is somewhat tongue-in-cheek but applies most of the time).
The usefulness of classes is when you have multiple methods that naturally share a set of data and you want to keep them together. Or if you have different types of objects that share common functionality and you want them to look the same externally.
It is mostly the same.
Some differences:
The function is available to be used outside the class.
If you include the calculation in the init constructor, the value is saved as an attribute of the class. It will not be recalculated every time you want to see the value.
With a class you should preferably do something like that :
class Point:
""" Create a new Point, at coordinates x, y """
def __init__(self, x=0, y=0):
""" Create a new point at x, y """
self.x = x
self.y = y
def distance(self):
self.z = ((self.x ** 2) + (self.y ** 2)) ** 0.5
return self.z
p = Point(3, 4)
print(p.distance())
The main advantage is to avoid the need of passing parameters to the method while they are required for a function
Here is the gist of it. A method can depend on the state (instance) variables in a class. A free function outside of a class has no such dependence.
Example:
>>> x = "foo"
foo
>>> x.length()
3
The method length() has no arguments; however you can see it is dependent on the string's state (its character sequence).
Hello im learning python and i get class topic.
I recived a error message like this:
"TypeError: __init__() missing 1 required positional argument: 'y'"
this is my code from shell:
class Vektor():
""" Bu bir vektör sınıfıdır"""
def __init__(self,x,y):
self.x = x
self.y = y
def boyu(self):
boy = (self.x**2+self.y**2)**0.5
return boy
def __repr__(self):
return ("%di + %dy" %(self.x, self.y))
def __sub__(self,digeri):
return Vektor(self.x - digeri.x, self.y - digeri.y)
def __add__(self,digeri):
return Vektor(self.x + digeri.x, self.y + digeri.y)
def __eq__(self,digeri):
if self.boyu() == digeri.boyu(): return True
def __mul__(self,digeri):
self.x = Vektor(self.x + digeri.x)
self.y = Vektor(self.y + digeri.y)
return Vektor(self.x*digeri.x,self.y*digeri.y)
When i try to create a C = A*B like this i get error:
TypeError: init() missing 1 required positional argument: 'y'
Im already thank you and i want to remind im a newbite in programming :))
How many arguments does Vektor.__init__ require? Ignoring self, it's two - x and y.
When you wrote return Vektor(self.x*digeri.x,self.y*digeri.y), you passed two arguments, so this works.
When you wrote self.x = Vektor(self.x + digeri.x), this doesn't work, because you don't pass a second argument for the y value.
When Python gave you the error, it should have included a line number, which is supposed to show where the error occurred. You didn't include that, but it was this line, wasn't it?
Since Vektor is supposed to contain two scalars and not sometimes replace them with two vectors, you could just write
self.x = self.x + digeri.x # still not a vector
self.y = self.y + digeri.y # also not a vector
but the more important lesson is to read the error message carefully. Everything you needed was there.
A note on operator semantics: since you wouldn't normally expect an expression like x = v * w to modify x, you shouldn't be mutating self inside the operator function anyway.
You return the resultant vector, which is enough. I showed you how to fix the syntax of those two lines, but you should really just remove them entirely.
And another note on vectors: overloading * isn't such an obvious win as it is for a scalar numeric type, because vectors usually have more than one possible type of product.
In __mul__ you do for some reason:
self.x = Vektor(self.x + digeri.x)
which is calling Vektor.__init__ providing only the positional argument x, with the value self.x + digeri.x, but nothing for y, thus the error.
Also this attempts to change the attribute x into an object from Vektor itself, I can't imagine that this is somehow what you want.
To me it is not clear what the 2 lines before the return statement in your __mul__ are supposed to do. Also both lines will produce the error you see.
Should your __mul__ be the dot product? If so, try:
return self.x*digeri.x + self.y*digeri.y
Another simplification to your class could be to allow iteration on your coords, like:
#property
def coords(self):
return self.x, self.y
def __iter__(self):
return self.coords.__iter__()
Then your dot product might just look like:
def dot(self, w):
"""
The dot product of self and other vector w.
"""
return sum([xi_s * xi_w for xi_s, xi_w in zip(self, w)])
There is VecPy, a very simple example class that does this kind of things. Maybe having a look can give you some more ideas.
The error is due to
self.x = Vektor(self.x + digeri.x)
When we call Vector like the way you write the syntax, it is thinking that you want to initialize it and it is expecting two inputs. Just get rid of the first two lines of mul function should fix the problem.
I try to process some data in Python and I defined a class for a sub-type of data. You can find a very simplified version of the class definition below.
class MyDataClass(object):
def __init__(self, input1, input2, input3):
"""
input1 and input2 are a 1D-array
input3 is a 2D-array
"""
self._x_value = None # int
self._y_value = None # int
self.data_array_1 = None # 2D array
self.data_array_2 = None # 1D array
self.set_data(input1, input2, input3)
def set_data(self, input1, input2, input3):
self._x_value, self._y_value = self.get_x_and_y_value(input1, input2)
self.data_array_1 = self.get_data_array_1(input1)
self.data_array_2 = self.get_data_array_2(input3)
#staticmethod
def get_x_and_y_value(input1, input2):
# do some stuff
return x_value, y_value
def get_data_array_1(self, input1):
# do some stuff
return input1[self._x_value:self._y_value + 1]
def get_data_array_2(self, input3):
q = self.data_array_1 - input3[self._x_value:self._y_value + 1, :]
return np.linalg.norm(q, axis=1)
I'm trying to follow the 'Zen of Python' and thereby to write beautiful code. I'm quite sceptic, whether the class definition above is a good pratice or not. While I was thinking about alternatives I came up with the following questions, to which I would like to kindly get your opinions and suggestions.
Does it make sense to define ''get'' and ''set'' methods?
IMHO, as the resulting data will be used several times (in several plots and computation routines), it is more convenient to create and store them once. Hence, I calculate the data arrays once in the constructor.
I do not deal with huge amount of data and therefore processing takes not more than a second, however I cannot estimate its potential implications on RAM if someone would use the same procedure for huge data.
Should I put the function get_x_and_y_value() out of the class scope and convert static method to a function?
As the method is only called inside the class definition, it is better to use it as a static method. If I should define it as a function, should I put all the lines relevant to this class inside a script and create a module of it?
The argument naming of the function get_x_and_y_value() are the same as __init__ method. Should I change it?
It would ease refactoring but could confuse others who read it.
In Python, you do not need getter and setter functions. Use properties instead. This is why you can access attributes directly in Python, unlike other languages like Java where you absolutely need to use getters and setters and to protect your attributes.
Consider the following example of a Circle class. Because we can use the #property decorator, we don't need getter and setter functions like other languages do. This is the Pythonic answer.
This should address all of your questions.
class Circle(object):
def __init__(self, radius):
self.radius = radius
self.x = 0
self.y = 0
#property
def diameter(self):
return self.radius * 2
#diameter.setter
def diameter(self, value):
self.radius = value / 2
#property
def xy(self):
return (self.x, self.y)
#xy.setter
def xy(self, xy_pair):
self.x, self.y = xy_pair
>>> c = Circle(radius=10)
>>> c.radius
10
>>> c.diameter
20
>>> c.diameter = 10
>>> c.radius
5.0
>>> c.xy
(0, 0)
>>> c.xy = (10, 20)
>>> c.x
10
>>> c.y
20
I was just wondering when to store things as part of a class instance versus when to use a method to return things. For example, which of the following would be better:
class MClass():
def __init__(self):
self.x = self.get_x()
self.get_y()
self.z = None
self.get_z()
def get_x(self):
return 2
def get_y(self):
self.y = 5 * self.x
def get_z(self):
return self.get_x() * self.x
What are the conventions regarding this sort of thing and when should I assign things to self and when should I return values? Is this essentially a public/private sort of distinction?
You shouldn't return anything from __init__.
Python is not Java. You don't need to include get for everything.
If x is always 2 and y is always 10 and z is always 12, that is a lot of code.
Making some assumptions, I would write that class:
class MClass(object):
def __init__(self, x):
self.x = x
def y(self):
return self.x * 5
def z(self):
return self.x + self.y()
>>> c = MClass(2)
>>> c.x
2
>>> c.y() # note parentheses
10
>>> c.z()
12
This allows x to change later (e.g. c.x = 4) and still give the correct values for y and z.
You can use the #property decorator:
class MClass():
def __init__(self):
self.x = 2
#property
def y(self):
return 5 * self.x
#here a plus method for the setter
#y.setter
def y(self,value):
self.x = y/5
#property
def z(self):
return self.x * self.x
It's a good way of organizing yours acessors
There's no "conventions" regarding this, AFAIK, although there're common practices, different from one language to the next.
In python, the general belief is that "everything is public", and there's no reason at all to have a getter method just to return the value of a instance variable. You may, however, need such a method if you need to perform operations on the instance when such variable is accessed.
Your get_y method, for example, only makes sense if you need to recalculate the expression (5 * self.x) every time you access the value. Otherwise, you should simply define the y variable in the instance in __init__ - it's faster (because you don't recalculate the value every time) and it makes your intentions clear (because anyone looking at your code will immediately know that the value does not change)
Finally, some people prefer using properties instead of writing bare get/set methods. There's more info in this question
I read your question as a general Object Oriented development question, rather than a python specific one. As such, the general rule of member data would be to save the data as a member of the class only if it's relevant as part of a particular instance.
As an example, if you have a Screen object which has two dimensions, height and width. Those two should be stored as members. The area associated with a particular instance would return the value associated with a particular instance's height and width.
If there are certain things that seem like they should be calculated on the fly, but might be called over and over again, you can cache them as members as well, but that's really something you should do after you determine that it is a valid trade off (extra member in exchange for faster run time).
get should always do what it says. get_y() and get_z() don't do that.
Better do:
class MClass(object):
def __init__(self):
self.x = 2
#property
def y(self):
return 5 * self.x
#property
def z(self):
return self.x * self.x
This makes y and z always depend on the value of x.
You can do
c = MClass()
print c.y, c.z # 10, 4
c.x = 20
print c.y, c.z # 100, 400
I am new to Python so please don't flame me if I ask something too noobish :)
1.
Consider I have a class:
class Test:
def __init__(self, x, y):
self.x = x
self.y = y
def wow():
print 5 * 5
Now I try to create an object of the class:
x = Test(3, 4)
This works as expected. However, when I try to call the method wow(), it returns an error, which is fixed by changing wow() to:
def wow(self)
Why do I need to include self and if I don't, what does the method mean?2. In the definition of __init__:
def __init__(self, x, y):
self.x = x
self.y = y
Why do I need to declare x and y, when I can do this:
def __init__(self):
self.x = x
self.y = y
I hope I am being clear...
Thanks for your time.
If you do that :
def __init__(self):
self.x = x
self.y = y
you assign the gobal vars x and y (it they exists ) to your instance
with :
def __init__(self, x, y):
self.x = x
self.y = y
you assign what you give as parameter to the constructor
and that is a lot more flexible :-)
The instance reference in Python is explicit. That way it can be manipulated by e.g. decorators before finally being passed to the method.
We need to declare x and y as arguments to the function so that we can use their names within the function, bound to the arguments passed in the corresponding function call.
Just to be clear
Why do I need to declare x and y, when
I can do this:
def __init__(self):
self.x = x
self.y = y
This ^ will only work if x and y can be found at runtime - if they haven't been passed in then they must have been set elsewhere (globally) or it will generate an error.
>>> class c:
def __init__(self):
self.x = x
>>> x = 1
>>> q = c()
>>> q.x
1
>>> del x
>>> q.x
1
>>> w = c()
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
w = c()
File "<pyshell#14>", line 3, in __init__
self.x = x
NameError: global name 'x' is not defined
>>>
>>> w = c(2)
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
w = c(2)
TypeError: __init__() takes exactly 1 argument (2 given)
This is why you want / need to specify them as parameters - it might work with the global lookup but it would probably violate the "principle of least astonishment"
self is a "magic" name - it can really be anything, but self is used for consistency and clarity. To answer your question, each class method/function requires an explicit reference to the class as the first parameter. Using Ipython:
In [66]: class Test:
....: def __init__(self):
....: pass
....: def wow(self):
....: print self
....:
....:
In [67]: x = Test()
In [68]: x.wow()
<__main__.Test instance at 0x0159FDF0>
Your second example won't actually work unless you already have an x and y in your namespace.
For instance, if you defined your class:
class Test:
def __init__(self):
self.x = x
self.y = y
and tried
x = Test()
it will throw a NameError.
However if you write:
x = 3
y = 4
test = Test()
then it will work. However, it's not a good idea to do such a thing. For the reason why read line 2:
In [72]: import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
In Python, methods should always take "one extra" argument, which is the reference to the instance the method is being called on. This is automatic in other languages such as Java, C#, etc. but Python is verbose about it.
That doesn't make sense. Where are x and y in that example? If you want the constructor to take two arguments which populate the object, define it as such. Otherwise, you're doing something else.
Python is different from languages like C++ and Java in that the object instance reference is passed explicitly.
That is, if you have an object which is an instance of the class and you want to invoke a method that operates on that instance (e.g., reads its fields), you use the self references as the object.
In c++ and Java, you have an implicit "this" reference that is present in the compiled version of your program but not in the source code. You use the static keyword to make it into a class method that does not have a "this".