class vs function ??? [英] class vs function ???

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

问题描述

嗨!


我是一个可怜的凡人,已经对Python感到害怕。好像

抛出了窗外的所有OO概念。身无分文,我问一个

基本问题:

Python中的类和函数有什么区别???

考虑以下代码片段:

#片段1开始


a = 1

打印一个

def fun1( ):

全球b

b ="里面的fun1"

打印b

fun1()

def c1():

打印一份

打印b

c = 12

def fun2 ():

global d

d ="我们死了!!!"

打印d

fun2()

c1()

class c2:

pass

print d

打印c


#片段1结束

#片段2开始

a = 1

打印一个

def fun1():

全球b

b ="里面的fun1"

打印b < br $>
fun1()

class c1:

打印一份

打印b

c = 12

def fun2():

全球d $ / $
d ="我们死了!!!"

打印d

fun2()

class c2:

通过


打印d

打印c

#Fragment 2结束


这两个代码的输出完全一样! (包括由于''print c''导致的

错误)


样本输出为:

1

里面的乐趣1

1

里面的乐趣1

我们死了!!!

我们是死了!!!

回溯(最近一次调用最后一次):

文件XYZ.py,第21行,在?

打印c

NameError:名称''c''未定义


等待一些''逻辑''解释!!!


Gaurav

Hi !

I am a poor mortal who has become terrified of Python. It seems to
have thrown all the OO concepts out of the window. Penniless, I ask a
basic question :
What is the difference between a class and a function in Python ???
Consider the following code fragments :
# Fragment 1 begins

a = 1
print a
def fun1():
global b
b = "inside fun1"
print b
fun1()
def c1():
print a
print b
c = 12
def fun2():
global d
d = "We are dead !!!"
print d
fun2()
c1()
class c2:
pass
print d
print c

# Fragment 1 ends
# Fragment 2 begins
a = 1
print a
def fun1():
global b
b = "inside fun1"
print b
fun1()
class c1:
print a
print b
c = 12
def fun2():
global d
d = "We are dead !!!"
print d
fun2()
class c2:
pass

print d
print c

# Fragment 2 ends

The output of both these codes are precisely the same ! (including the
error because of ''print c'')

The sample output is :
1
inside fun1
1
inside fun1
We are dead !!!
We are dead !!!
Traceback (most recent call last):
File "XYZ.py", line 21, in ?
print c
NameError: name ''c'' is not defined

Waiting for some ''logical'' explanation !!!

Gaurav

推荐答案

Python允许你基本上做任何你想做的事情,这并不意味着

,你所做的事情是有意义的。


有些情况下嵌入功能很有用(你不希望它有/ b $ b功能可用于其他一切在模块中),但它没有帮助数据抽象,这是类的基本用法:
Python allows you to do basically whatever you want, that doesn''t mean
that what you do makes sense.

There are cases where embedding functions is useful (you don''t want that
function available to everything else in the module), but it doesn''t
help with data abstraction, which is the fundamental use of classes:
class blah:
.... def __init __(self,embed):

.... self.embedded = embed

.... def print_embedded(self):

.... print self.embedded

.... a = blah(5)
a.print_embedded()
5


要使用嵌入式函数执行此操作,您需要添加属性

一些明确返回的对象。

def blah2(嵌入):
.... def print_embedded():

.... print embed

.... print_embedded.print_embedded = print_embedded

.... return print_embedded

.... b = blah2(6)
b .print_embedded()
class blah: .... def __init__(self, embed):
.... self.embedded = embed
.... def print_embedded(self):
.... print self.embedded
.... a = blah(5)
a.print_embedded() 5

To do this with embedded functions, you''d need to add an attribute to
some returned object explicitly.
def blah2(embed): .... def print_embedded():
.... print embed
.... print_embedded.print_embedded = print_embedded
.... return print_embedded
.... b = blah2(6)
b.print_embedded()


6

现在,上述工作,但在我看来,是丑陋的。我建议你尝试使用嵌入式功能执行以下操作
(可以做,但很难看):


class blah3:

通过

c = blah3()

c.hello = 1

c.goodbye = 2

del c.hello

print c.goodbye


Python中的类和函数有什么区别???

6
Now, the above works, but in my opinion, is ugly. I suggest you try to
do the following with embedded functions (it can be done, but is ugly):

class blah3:
pass
c = blah3()
c.hello = 1
c.goodbye = 2
del c.hello
print c.goodbye

What is the difference between a class and a function in Python ???



从功能上讲,两者都可以这样做。实际上,类更容易用于所有非平凡的数据抽象。我知道我的

解释并不是很好,但你真的没有看多少

Python代码如果你不明白它们之间的区别功能和

课程。


- Josiah



Functionally, you can do the same with both. Pragmatically, classes are
easier to use for all non-trivial data abstraction. I know my
explanation hasn''t been very good, but you really haven''t looked at much
Python code if you don''t understand the difference between functions and
classes.

- Josiah


" Gaurav Veda" < GV *** @ iitk.ac.in>在留言中写道

新闻:1c ************************** @ posting.google.c om ...

[snip]
"Gaurav Veda" <gv***@iitk.ac.in> wrote in message
news:1c**************************@posting.google.c om...
[snip]
a = 1
打印
def fun1():
全局b
b =" ;里面的fun1"
打印b
fun1()
def c1():
打印一份
打印b
c = 12
def fun2():
全球d
d =我们死了!!!
打印d
fun2()
c1()
class c2:
打印
打印c
[snip] Traceback(最近一次调用最后一次):
文件XYZ.py,第21行,在?
打印c
NameError:名称''c''未定义

等待一些''逻辑''解释!!!
a = 1
print a
def fun1():
global b
b = "inside fun1"
print b
fun1()
def c1():
print a
print b
c = 12
def fun2():
global d
d = "We are dead !!!"
print d
fun2()
c1()
class c2:
pass
print d
print c [snip] Traceback (most recent call last):
File "XYZ.py", line 21, in ?
print c
NameError: name ''c'' is not defined

Waiting for some ''logical'' explanation !!!



错误消息_is_逻辑解释:


名称''''尚未在范围内定义

''print c''被调用。


你有一个变量'c''在代码中较早,是的,但是

c是函数c1()的局部变量,即名称''c''_is_

已定义在该函数的范围内(但_only_在那个

范围内)。它在程序范围内没有被定义,这是你尝试使用它的地方。


我会避免越过Python'这里的范围规则,他们可以在其他地方找到
。祝你好运,


Sean



The error message _is_ the logical explanation:

The name ''c'' has not been defined in the scope where
''print c'' is called.

You have a variable ''c'' earlier in the code, yes, but that
c is a local variable of function c1(), i.e., the name ''c'' _is_
defined in the scope of that function (but _only_ in that
scope). It is _not_ defined at the program scope, which
is where you''ve tried to use it.

I''ll avoid going over Python''s scoping rules here, they can
be found elsewhere. Good luck,

Sean




" Gaurav Veda" < GV *** @ iitk.ac.in>在留言中写道

新闻:1c ************************** @ posting.google.c om ...

"Gaurav Veda" <gv***@iitk.ac.in> wrote in message
news:1c**************************@posting.google.c om...
嗨!

我是一个可怜的凡人,已经对Python感到害怕。它似乎已经把所有的OO概念抛到了窗外。身无分文,我问一个基本问题:
Python中的类和函数有什么区别?
考虑以下代码片段:
#Fragment 1开始< br =>
a = 1
打印
def fun1():
全局b
b =里面的fun1
打印b
fun1()
def c1():
打印一份
打印b
c = 12
def fun2():
global d
d =我们死了!!!
打印d
fun2()
c1()
类c2:
传递
打印c

#片段1结束
#Fragment 2开始
a = 1
打印
def fun1():
全球b
b =内部fun1
打印b
fun1()
类c1:
打印
打印b < br => c = 12
def fun2():
全球d
d =我们死了!!!
打印d
fun2()
class c2:
传递

打印d
打印c

#Fragment 2 ends

这两个代码的输出完全一样! (包括因''打印c''而导致的错误)


为什么它们不一样?

样本输出是:
1
里面的fun1
1
里面的乐趣1
我们死了!!!
我们死了!!!
追溯(最近的呼叫最后一次):
文件XYZ.py,第21行,在?
打印c
NameError:名称''c''未定义



可能让你感到困惑的是

当班级为

时班级的文本执行定义,而函数的文本不是。

所以在这两种情况下执行相同的语句,

但由于执行顺序不同。

a = 1
打印一个< ------" 1"
def fun1():
全局b
b =" inside fun1"
打印b
fun1()< ---"里面的fun1"
def c1():
打印一份
打印b
c = 12
def fun2():
全球d
d =我们死了!!!
打印d
fun2()
c1 ()< ----" 1"," inside fun1",我们死了!!! 类c2:
传递
打印d< - ----我们死了!!!
a = 1
打印一个< ------" 1"
def fun1():
全局b
b =" inside fun1"
打印b
fun1()< ---"里面的fun1"
类c1():
打印一个< ---" 1"
打印b< ---"里面的fun1"
c = 12
def fun2():
全球d
d =我们死了!!! ;
打印d< ----我们死了!!!"
fun2()
类c2:
传递
打印d< -----我们死了!!!


John Roth

Gaurav
Hi !

I am a poor mortal who has become terrified of Python. It seems to
have thrown all the OO concepts out of the window. Penniless, I ask a
basic question :
What is the difference between a class and a function in Python ???
Consider the following code fragments :
# Fragment 1 begins

a = 1
print a
def fun1():
global b
b = "inside fun1"
print b
fun1()
def c1():
print a
print b
c = 12
def fun2():
global d
d = "We are dead !!!"
print d
fun2()
c1()
class c2:
pass
print d
print c

# Fragment 1 ends
# Fragment 2 begins
a = 1
print a
def fun1():
global b
b = "inside fun1"
print b
fun1()
class c1:
print a
print b
c = 12
def fun2():
global d
d = "We are dead !!!"
print d
fun2()
class c2:
pass

print d
print c

# Fragment 2 ends

The output of both these codes are precisely the same ! (including the
error because of ''print c'')
Why shouldn''t they be the same?

The sample output is :
1
inside fun1
1
inside fun1
We are dead !!!
We are dead !!!
Traceback (most recent call last):
File "XYZ.py", line 21, in ?
print c
NameError: name ''c'' is not defined

Waiting for some ''logical'' explanation !!!

What has probably gotten you confused is that
the text of the class executes when the class is
defined, while the text of a function doesn''t.
So in both cases the same statements executed,
but because of a different execution order.
a = 1
print a <------ "1"
def fun1():
global b
b = "inside fun1"
print b
fun1() <--- "inside fun1"
def c1():
print a
print b
c = 12
def fun2():
global d
d = "We are dead !!!"
print d
fun2()
c1() <---- "1", "inside fun1", "We are dead!!!"
class c2:
pass
print d <----- "we are dead !!!" a = 1
print a <------ "1"
def fun1():
global b
b = "inside fun1"
print b
fun1() <--- "inside fun1"
class c1():
print a <--- "1"
print b <--- "inside fun1"
c = 12
def fun2():
global d
d = "We are dead !!!"
print d <---- "we are dead !!!"
fun2()
class c2:
pass
print d <----- "we are dead !!!"
John Roth

Gaurav



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

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