简体   繁体   English

Python 中的空 class object

[英]Empty class object in Python

I'm teaching a Python class on object-oriented programming and as I'm brushing up on how to explain classes, I saw an empty class definition:我正在教授 Python class 面向对象编程,当我复习如何解释课程时,我看到了一个空的 class 定义:

class Employee:
    pass

The example then goes on to define a name and other attributes for an object of this class:然后该示例继续为此 class 的 object 定义名称和其他属性:

john = Employee()
john.full_name = "john doe"

Interesting!有趣的!

I'm wondering if there's a way to dynamically define a function for an instance of a class like this?我想知道是否有办法为这样的 class 的实例动态定义 function ? something like:就像是:

john.greet() = print 'Hello, World!'

This doesn't work in my Python interpreter, but is there another way of doing it?这在我的 Python 解释器中不起作用,但是还有其他方法吗?

A class is more or less a fancy wrapper for a dict of attributes to objects. class 或多或少是对象属性dict的精美包装器。 When you instantiate a class you can assign to its attributes, and those will be stored in foo.__dict__ ;当您实例化 class 时,您可以为其分配属性,这些属性将存储在foo.__dict__ likewise, you can look in foo.__dict__ for any attributes you have already written.同样,您可以在foo.__dict__中查找您已经编写的任何属性。

This means you can do some neat dynamic things like:这意味着您可以执行一些简洁的动态操作,例如:

class Employee: pass
def foo(self): pass
Employee.foo = foo

as well as assigning to a particular instance.以及分配给特定实例。 (EDIT: added self parameter) (编辑:添加self参数)

Try with lambda :尝试使用lambda

john.greet = lambda : print( 'hello world!' )

The you'll be able to do:你将能够做到:

john.greet()

EDIT : Thanks Thomas K for the note - this works on Python 3.2 and not for Python2, where print appeared to be statement .编辑:感谢Thomas K的注释 - 这适用于Python 3.2而不是 Python2,其中print似乎是statement But this will work for lambda s, without statements (right? Sorry, I know only python3.2 (: )但这适用于lambda s,没有语句(对吧?对不起,我只知道python3.2 (:)

You could also use "named tuples" from the collection standard module.您还可以使用collection标准模块中的“命名元组”。 Named tuples work like "ordinary" tuples but the elements have names and you can access the elements using the "dot syntax".命名元组像“普通”元组一样工作,但元素有名称,您可以使用“点语法”访问元素。 From the collection docs :集合文档

>>> # Basic example
>>> Point = namedtuple('Point', ['x', 'y'])
>>> p = Point(11, y=22)     # instantiate with positional or keyword arguments
>>> p[0] + p[1]             # indexable like the plain tuple (11, 22)
33
>>> x, y = p                # unpack like a regular tuple
>>> x, y
(11, 22)
>>> p.x + p.y               # fields also accessible by name
33
>>> p                       # readable __repr__ with a name=value style
Point(x=11, y=22)

You could use AttrDict你可以使用 AttrDict

>>> from attrdict import AttrDict
>>> my_object = AttrDict()
>>> my_object.my_attribute = 'blah'
>>> print my_object.my_attribute
blah
>>> 

Install attrdict from PyPI:从 PyPI 安装 attrdict:

pip install attrdict 

It's useful in other situations too - like when you need attribute access to dict keys.它在其他情况下也很有用——比如当您需要对 dict 键进行属性访问时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM