简单的Python类问题 [英] Simple Python class questions

查看:54
本文介绍了简单的Python类问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个Python新手,但是对类,对象

等如何在VB.Net中工作有一些基本的了解。但是,我正在努力将这些知识转化为Python语境。


我正在努力教自己这方面的Python通过试用

项目,其中一部分要求定期从串行数据中提取数据

连接。将所有

通信程序/函数放在他们自己的类和模块中并从主实例化对象中调用这些函数看起来很明智

控制模块。但是我很难让这个工作 - 不确定

是否是对使用类的基本误解?
Python,语法错误纯粹和简单或,最有可能的是两个组合




也许我可以提供一些大纲代码作为插图:


假设我在一个名为comms.py的模块中定义了类。这个班级

并不是真的会继承任何其他类别(大概是基本意义上的原因除了b $ b,这可能是自动的

并隐含使用class关键字)。我们打电话给这个班级

serial_link。所以在comms.py中我有:


class serial_link:

def __init __(self):

试试

导入序列号#seserial库

除了ImportException

#Error处理


def openPort(self):

尝试

#Code尝试打开串口

返回成功

除了SerialException

返回失败


然后在我单独的主要呼叫模块中,我可能有:


导入通讯

serlink = comms.seral_link #Create serial_link class的实例

print serlink.openPort

最后一行我希望打印成功或失败。但我只是

似乎得到了封闭的openPort函数的内部引用

in<> ;.


那么为什么不行吗?我可能在这里犯了多个错误

但据我所知,语法似乎正确。对于这个特殊的

示例,我不需要传递来自

''seriallink.openPort'函数的任何参数,所以没有包含任何括号(或者

他们是强制性的,即使是空的吗?)我可以继续解释

等,但最简单的看看是否有人能发现吼声

直接退出。


愿意为任何人提供帮助的TIA

解决方案

Le Thursday 19 2008年6月13:54:03 John Dann,vousavezécrit*:


Python新手,但对类,对象的基本理解

等在VB.Net中工作。但是,我正在努力将这些知识转化为Python语境。


也许我可以提供一些大纲代码作为例证:


假设我在一个名为comms.py的模块中定义了这个类。这个班级

并不是真的会继承任何其他类别(大概是基本意义上的原因除了b $ b,这可能是自动的

并隐含使用class关键字)。



不是不是:)建议总是使用新式的类,因此给b $ b $给出对象库明确地说:


class serial_link(object):

...


http://docs.python.org/ref/node33.html
< blockquote class =post_quotes>
print serlink.openPort



你刚忘了(),所以你打印方法对象本身没有

调用它:print serlink.openPort()


-
$ b $bCédricLucantis


6月19日,6:54 * pm,John Dann< n ... @ prodata.co.ukwrote:


一个Python新手,但是一些基本的理解如何在VB.Net中运行类,对象和b $ b等工作。但是,我正在努力将这些知识转化为Python语境。


我正在努力教自己这方面的Python通过试用

项目,其中一部分要求定期从串行数据中提取数据

连接。将所有

通信程序/函数放在他们自己的类和模块中并从主实例化对象中调用这些函数看起来很明智

控制模块。但是我很难让这个工作 - 不确定

是否是对使用类的基本误解?
Python,语法错误纯粹和简单或,最有可能的是两个组合




也许我可以提供一些大纲代码作为插图:


假设我在一个名为comms.py的模块中定义了类。这个班级

并不是真的会继承任何其他类别(大概是基本意义上的原因除了b $ b,这可能是自动的

并隐含使用class关键字)。我们打电话给这个班级

serial_link。所以在comms.py中我有:


class serial_link:

* * * * def __init __(self):

* * * * * * * *尝试

* * * * * * * * * * * *导入序列号#seserial库

* * * * * * * *除了ImportException

* * * * * * * * * * * * #Error处理


* * * * def openPort(self):

* * * * * * * *尝试

* * * * * * * * * * * * #Code尝试打开串口

* * * * * * * * * * * *返回成功

* * * * * * * *除了SerialException

* * * * * * * * * * * *返回失败


然后在我单独的主呼叫模块中,我可能有:


导入通信

serlink = comms.seral_link * * #Create serial_link class的实例

print serlink.openPort


最后一行我希望能打印成功或失败。但我只是

似乎得到了封闭的openPort函数的内部引用

in<> ;.


那么为什么不行吗?我可能在这里犯了多个错误

但据我所知,语法似乎正确。对于这个特殊的

示例,我不需要传递来自

''seriallink.openPort'函数的任何参数,所以没有包含任何括号(或者

他们是强制性的,即使是空的吗?)我可以继续解释

等,但最简单的看看是否有人能发现吼声

直接出局。



是的,即使没有参数,它们也是强制性的,这是必需的,所以

python可以区分调用函数和传递函数

对象。


愿意帮助
的人的TIA


John Dann写道:


假设我在一个名为comms.py的模块中定义该类。这个班级

并不是真的会继承任何其他类别(大概是基本意义上的原因除了b $ b,这可能是自动的

并隐含使用class关键字)。我们打电话给这个班级

serial_link。所以在comms.py中我有:


class serial_link:

def __init __(self):

试试

导入序列号#seserial库



停止,这不行。除了VB之外,Python实际上是区分大小写的,所以

你必须写''try''而不是''尝试''和''import''而不是''Import''。

此外,导致缩进的许多(所有?)语句通常是以冒号结尾的
,所以就像''class ..:''和''def。 。:''你还要

必须使用''try:''而不仅仅是''尝试''。修复所有这些并再试一次,我猜

这已经有很多帮助了。


还有一件事:你在滥用异常。通常,在如此短的

程序中,您只在主条目函数中有一个try-except对,而

所有其他代码仅抛出异常。特别是类的__init__

函数应始终使用异常来表示错误。但是,

这不是一个严格的是/否问题,而是一个风格的问题。

Uli


-

Sator Laser GmbH

Gesch?¤ftsf?? hrer:Thorsten F ?? cking,Amtsgericht Hamburg HR B62 932


A Python newbie, but some basic understanding of how classes, objects
etc work in eg VB.Net. However, I''m struggling a little to translate
this knowledge into the Python context.

I''m trying to teach myself this aspect of Python by working up a trial
project, part of which calls for pulling in data from a serial data
connection at regular intervals. It looked sensible to place all the
comms procedures/functions in their own class and module and make
calls to those functions from an object instantiated in a main
controlling module. But I''m struggling to get this working - not sure
whether it''s a fundamental misunderstanding of the use of classes in
Python, syntax errors pure and simple or, most likely, a combination
of both!

Maybe I could provide some outline code as an illustration:

Let''s say I define the class in a module called comms.py. The class
isn''t really going to inherit from any other class (except presumably
in the most primitive base-class sense, which is presumably automatic
and implicit in using the class keyword). Let''s call the class
serial_link. So in comms.py I have:

class serial_link:
def __init__(self):
Try
Import serial # the pyserial library
Except ImportException
#Error handling

def openPort(self):
Try
#Code to try opening serial port
Return "Success"
Except SerialException
Return "Failure"

Then in my separate main calling module I might have:

Import comms
serlink=comms.seral_link #Create instance of serial_link class
print serlink.openPort
The last line I''m hoping would print Success or Failure. But I just
seem to get some internal reference to the openPort function enclosed
in <>.

So why doesn''t it work please? I may be making multiple errors here
but as far as I can see the syntax seems right. For this particular
example, I don''t need to pass any arguments from the
''seriallink.openPort'' function so haven''t included any parentheses (or
are they mandatory even if empty?) I could go on with the explanations
etc, but it may be simplest to see if anyone can spot a howler
straight out.

TIA for anyone willing to help

解决方案

Le Thursday 19 June 2008 13:54:03 John Dann, vous avez écrit*:

A Python newbie, but some basic understanding of how classes, objects
etc work in eg VB.Net. However, I''m struggling a little to translate
this knowledge into the Python context.

Maybe I could provide some outline code as an illustration:

Let''s say I define the class in a module called comms.py. The class
isn''t really going to inherit from any other class (except presumably
in the most primitive base-class sense, which is presumably automatic
and implicit in using the class keyword).

No it''s not :) It is recommended to always use new-style classes, and thus to
give the object base explicitely :

class serial_link (object) :
...

see http://docs.python.org/ref/node33.html

print serlink.openPort

You just forgot the (), so you''re printing the method object itself without
calling it : print serlink.openPort()

--
Cédric Lucantis


On Jun 19, 6:54*pm, John Dann <n...@prodata.co.ukwrote:

A Python newbie, but some basic understanding of how classes, objects
etc work in eg VB.Net. However, I''m struggling a little to translate
this knowledge into the Python context.

I''m trying to teach myself this aspect of Python by working up a trial
project, part of which calls for pulling in data from a serial data
connection at regular intervals. It looked sensible to place all the
comms procedures/functions in their own class and module and make
calls to those functions from an object instantiated in a main
controlling module. But I''m struggling to get this working - not sure
whether it''s a fundamental misunderstanding of the use of classes in
Python, syntax errors pure and simple or, most likely, a combination
of both!

Maybe I could provide some outline code as an illustration:

Let''s say I define the class in a module called comms.py. The class
isn''t really going to inherit from any other class (except presumably
in the most primitive base-class sense, which is presumably automatic
and implicit in using the class keyword). Let''s call the class
serial_link. So in comms.py I have:

class serial_link:
* * * * def __init__(self):
* * * * * * * * Try
* * * * * * * * * * * * Import serial # the pyserial library
* * * * * * * * Except ImportException
* * * * * * * * * * * * #Error handling

* * * * def openPort(self):
* * * * * * * * Try
* * * * * * * * * * * * #Code to try opening serial port
* * * * * * * * * * * * Return "Success"
* * * * * * * * Except SerialException
* * * * * * * * * * * * Return "Failure"

Then in my separate main calling module I might have:

Import comms
serlink=comms.seral_link * * #Create instance of serial_link class
print serlink.openPort

The last line I''m hoping would print Success or Failure. But I just
seem to get some internal reference to the openPort function enclosed
in <>.

So why doesn''t it work please? I may be making multiple errors here
but as far as I can see the syntax seems right. For this particular
example, I don''t need to pass any arguments from the
''seriallink.openPort'' function so haven''t included any parentheses (or
are they mandatory even if empty?) I could go on with the explanations
etc, but it may be simplest to see if anyone can spot a howler
straight out.

Yes they''re mandatory even if there is no arguments, this is needed so
python can differentiate between calling function and passing function
objects.

TIA for anyone willing to help


John Dann wrote:

Let''s say I define the class in a module called comms.py. The class
isn''t really going to inherit from any other class (except presumably
in the most primitive base-class sense, which is presumably automatic
and implicit in using the class keyword). Let''s call the class
serial_link. So in comms.py I have:

class serial_link:
def __init__(self):
Try
Import serial # the pyserial library

Stop, this can''t work. Other than VB, Python actually is case sensitive, so
you must write ''try'' and not ''Try'' and also ''import'' and not ''Import''.
Further, many (all?) statements that cause an indention are usually
terminated with a colon, so like with ''class ..:'' and ''def ..:'' you also
must use ''try:'' and not just ''try''. Fix all these and try again, I guess
this will already help a lot.

One more thing: you are abusing exceptions. Typically, in such a short
program you only have one try-except pair in the main entry function and
all other code only throws the exceptions. In particular the __init__
function of a class should always signal errors using exceptions. However,
this is not a strict yes/no question but rather a stylistic one.
Uli

--
Sator Laser GmbH
Gesch?¤ftsf??hrer: Thorsten F??cking, Amtsgericht Hamburg HR B62 932


这篇关于简单的Python类问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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