我的想法是Pythonic吗? [英] Is my thinking Pythonic?

查看:45
本文介绍了我的想法是Pythonic吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,

好​​吧,你们现在都知道,我正在学习Python :)

令人讨厌的一件事就是Python中的OOP。

在Java中考虑这个代码:

-

公共类汽车{

private int speed;

私人字符串品牌;

// setters&吸气员

}

-

只要看一下班级的顶部,就可以知道每个

实例有两个实例变量(速度和品牌)。

我试图转换成Python:

-

class Car:

def setspeed(自我,速度):

self.speed =速度

def setbrand(自我,品牌):

self.brand = brand

-

如果你有一个庞大的课程,你就无法想象
$ b $的实例变量b每个对象。

所以,我创建了这个构造函数:

-

def __init __(个体经营):

self.speed =无

self.brand =无

-

这样,我可以通过阅读

__init__方法。

您如何看待我的方法?它被认为是Pythonic吗?

有什么建议吗?

谢谢大家。

Hey,
Well, as you all know by now, I''m learning Python :)
One thing that is annoying my is the OOP in Python.
Consider this code in Java:
--
public class Car {
private int speed;
private String brand;
// setters & getters
}
--
With one look at the top of the class, you can know that each
instance has two instance variables (speed & brand).
I tried to transform in into Python:
--
class Car:
def setspeed(self, speed):
self.speed = speed
def setbrand(self, brand):
self.brand = brand
--
If you have a huge class, you can''t figure the instance variables of
each object.
So, I created this constructor:
--
def __init__(self):
self.speed = None
self.brand = None
--
This way, I can figure the instance variables by just reading the
__init__ method.
What do you think of my approach? is it considered Pythonic?
Any suggestions?
Thank you all.

推荐答案

侯赛因B:
Hussein B:

class Car:

def setspeed(self,speed):

self.speed = speed

def setbrand(self,brand):

self.brand = brand
class Car:
def setspeed(self, speed):
self.speed = speed
def setbrand(self, brand):
self.brand = brand



您还可以学习_attribute和__attribute约定。

在Python中,getter / setter的使用频率较低,你可以删除那两个

setter并只从外部访问属性。

后来,在派生类中,如果你想让他们的工作更复杂,你可以添加属性。

注意Python不能内联东西正如HotSpot所做的那样,每个

getter / setter(或者dot:foo.bar.baz比foo.bar慢)你使用

你的代码会慢下来。 />

You can also learn the _attribute and __attribute conventions.
In Python getter/setters are used less often, you can remove those two
setters and just access the attributes from outside.
Later, in derived classes, if you want to make their workings more
complex you can add properties.
Note that Python isn''t able to inline things as HotSpot does, so each
getter/setter (or dot: foo.bar.baz is slower than foo.bar) you use
your code slows down.


所以,我创建了这个构造或者:

-

def __init __(个体经营):

self.speed =无

self.brand =无

-

这样,我可以通过阅读

__init__方法来计算实例变量。
So, I created this constructor:
--
def __init__(self):
self.speed = None
self.brand = None
--
This way, I can figure the instance variables by just reading the
__init__ method.



有时我做同样的事情,去记录使用的属性。


再见,

熊宝宝

Sometimes I do the same thing, to to "document" the attributes used.

Bye,
bearophile


Hussein B写道:
Hussein B wrote:

嘿,

好​​吧,你们现在都知道,我正在学习Python :)

有一点是令人讨厌的是我的Python是OOP。

在Java中考虑这个代码:

-

公共类汽车{

private int speed;

private String品牌;

// setters&吸气员

}

-

只要看一下班级的顶部,就可以知道每个

实例有两个实例变量(速度和品牌)。

我试图转换成Python:

-

class Car:

def setspeed(自我,速度):

self.speed =速度

def setbrand(自我,品牌):

self.brand = brand

-

如果你有一个庞大的课程,你就无法想象
$ b $的实例变量b每个对象。

所以,我创建了这个构造函数:

-

def __init __(个体经营):

self.speed =无

self.brand =无

-

这样,我可以通过阅读

__init__方法。

您如何看待我的方法?它被认为是Pythonic吗?

有什么建议吗?
Hey,
Well, as you all know by now, I''m learning Python :)
One thing that is annoying my is the OOP in Python.
Consider this code in Java:
--
public class Car {
private int speed;
private String brand;
// setters & getters
}
--
With one look at the top of the class, you can know that each
instance has two instance variables (speed & brand).
I tried to transform in into Python:
--
class Car:
def setspeed(self, speed):
self.speed = speed
def setbrand(self, brand):
self.brand = brand
--
If you have a huge class, you can''t figure the instance variables of
each object.
So, I created this constructor:
--
def __init__(self):
self.speed = None
self.brand = None
--
This way, I can figure the instance variables by just reading the
__init__ method.
What do you think of my approach? is it considered Pythonic?
Any suggestions?



方法完全正确 - 在__init __-方法中声明的实例变量应该是(不必,但是b $ b) 。


但是,你*在定义getter和setter时是* unpythonic。这些是一个主要存在的原因,因为java没有属性概念

或委托,因此无法将代码添加到实例变量赋值中。 />
因此需要将* all *变量包装到get / set-pairs中,这样在行为改变的情况下(例如lazyfying)

的情况下,不需要改变

客户代码。


也许这个读数可以帮助你调整心态:

http://dirtsimple.org/2004/12/python-is-not-java。 html

Diez

The approach is exactly right - instance variable should be (don''t have to,
though) declared inside the __init__-method.

However, you *are* unpythonic in defining getters and setters. These are a
java-atrocity that mainly exists because java has no concept of properties
or delegates, and thus can''t add code to instance variable assignments.
Thus one needs to wrap *all* variables into get/set-pairs, such that in the
case of a behavior-change (e.g. lazyfying) one does not need to change
client-code.

Maybe this reading will help you adjust your mindset:

http://dirtsimple.org/2004/12/python-is-not-java.html
Diez


Hussein Baécrit:
Hussein B a écrit :

嘿,

好​​吧,就像你现在都知道的那样,我正在学习Python :)

令人讨厌的一件事就是Python中的OOP。
Hey,
Well, as you all know by now, I''m learning Python :)
One thing that is annoying my is the OOP in Python.



如果是这样,你问题的答案是显然,不是。 ! - )


好​​的,让我们看看......

If so, the answer to your question is "obviously, no" !-)

Ok, let''s see...


在Java中考虑这段代码:

-

公共汽车{

私人速度;

私人弦乐品牌;

// setters&吸气员

}

-

只要看一下班级的顶部,就可以知道每个

实例有两个实例变量(速度和品牌)。

我试图转换成Python:

-

class Car:
Consider this code in Java:
--
public class Car {
private int speed;
private String brand;
// setters & getters
}
--
With one look at the top of the class, you can know that each
instance has two instance variables (speed & brand).
I tried to transform in into Python:
--
class Car:



除非你有令人信服的理由,否则请使用新式课程:


class Car(对象)

Unless you have a compelling reason, use new-style classes:

class Car(object)


def setspeed(self,speed):

self.speed = speed

def setbrand(self,brand):

self.brand = brand
def setspeed(self, speed):
self.speed = speed
def setbrand(self, brand):
self.brand = brand



Java,C ++等需要getter和setter,因为它们没有支持

for计算属性,因此您无法在不破坏代码的情况下将普通属性转换为

计算的属性。 Python对计算的

属性有很好的支持,所以你不需要这些getter和setter。

pythonic翻译将是:


class Car(对象):

def __init __(自我,速度,品牌):

self.speed =速度

self.brand =品牌

Java, C++ etc require getters and setters because they have no support
for computed attributes, so you cannot turn a plain attribute into a
computed one without breaking code. Python has good support for computed
attributes, so you just don''t need these getters and setters. The
pythonic translation would be:

class Car(object):
def __init__(self, speed, brand):
self.speed = speed
self.brand = brand


如果你有一个庞大的班级,你无法计算每个对象的实例变量


If you have a huge class, you can''t figure the instance variables of
each object.



如果你的课程那么庞大,那么可能是重构

和/或重新考虑你的设计的时候了。 />

If your class is that huge, then it''s probably time to either refactor
and/or rethink your design.


所以,我创建了这个构造函数:
So, I created this constructor:



< mode =" pedantic">

s / constructor / initialiser /

< / mode>

<mode="pedantic">
s/constructor/initialiser/
</mode>


-

def __init __(个体经营):

self.speed =无

self.brand =无
--
def __init__(self):
self.speed = None
self.brand = None



如果我可能会问:为什么你不把速度和品牌作为参数?如果你想要允许没有参数的
,你总是可以使用默认值,即:


class Car(对象):

def __init __(自我,速度=无,品牌=无):

self.speed =速度

self.brand =品牌


这样,我可以通过阅读

__init__方法来计算实例变量。

您如何看待?我的做法?它被认为是Pythonic吗?
This way, I can figure the instance variables by just reading the
__init__ method.
What do you think of my approach? is it considered Pythonic?



就我而言 - 模拟关于initiliser'

params的问题 - 我认为设置好的风格尽可能在初始化程序中使用所有实例属性

合理的默认值,因此,当你说b
时,你不必浏览整个代码就知道了什么'可用。


现在记住Python对象(好吧,至少大部分都是)动态的
,所以可以在class语句之外添加属性身体。

As far as I''m concerned - and modulo the question about initiliser''s
params - I consider good style to set all instance attributes to
sensible default values in the initializer whenever possible, so, as you
say, you don''t have to browse the whole code to know what''s available.

Now remember that Python objects (well, most of them at least) are
dynamic, so attributes can be added outside the class statement body.


这篇关于我的想法是Pythonic吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆