GVKun编程网logo

确定变量的类型为python中的NoneType(python的变量,如何确定其类型和能进行的操作?)

14

在本文中,我们将给您介绍关于确定变量的类型为python中的NoneType的详细内容,并且为您解答python的变量,如何确定其类型和能进行的操作?的相关问题,此外,我们还将为您提供关于Python

在本文中,我们将给您介绍关于确定变量的类型为python中的NoneType的详细内容,并且为您解答python的变量,如何确定其类型和能进行的操作?的相关问题,此外,我们还将为您提供关于Python 3.x中的NoneType在哪里?、python – NoneType和str的不支持的操作数类型、Python 类:TypeError:'NoneType' 对象不可调用、Python3基础 type 获得变量的类型的知识。

本文目录一览:

确定变量的类型为python中的NoneType(python的变量,如何确定其类型和能进行的操作?)

确定变量的类型为python中的NoneType(python的变量,如何确定其类型和能进行的操作?)

我想检查变量是否为NoneType类型。对于其他类型,我们可以执行以下操作:

    type([])==list

但是对于NoneType这种简单的方法是不可能的。也就是说,我们不能说type(None)==NoneType。还有其他方法吗?为何某些类型而不是其他类型可能呢?谢谢。

Python 3.x中的NoneType在哪里?

Python 3.x中的NoneType在哪里?

在Python 3中,我想检查是否value为string或None

一种方法是

assert type(value) in { str, NoneType }

但是NoneTypePython在哪里?

无需任何导入,即可使用NoneTypeProduces NameError: name ''NoneType'' is not defined

答案1

小编典典

types.NoneType
在Python 3.10中重新引入。

Python 3.10的新增功能

改进模块

类型

重新引入types.EllipsisTypetypes.NoneTypetypes.NotImplementedType类,提供了一组新的通过类型跳棋容易解释。(由Bas
van Beek在bpo-41810中贡献。)

对变更的讨论是出于对需求的推动types.EllipsisType,因此types.NoneType也增加了一致性。

python – NoneType和str的不支持的操作数类型

python – NoneType和str的不支持的操作数类型

我收到一条消息说:

Traceback (most recent call last):
 File "/var/www/fosa/error_supressor.py",line 46,in <module>
   sys.stderr.write(latest + '\n')
TypeError: unsupported operand type(s) for +: 'nonetype' and 'str'

我已经试图解决这个问题好几天了,但说实话,我是一个多才多艺的程序员.
因此,让我们排队问题,看看是否有一个耐心的人可以节省一些时间来解决一个谦虚的陌生人的问题:-)

除此之外,当我检查我的错误日志时,我发现此错误消息,我怀疑是相关的:

File "/var/www/fosa/app/controllers/client/client.py",line 601,in detail
    if not course.bookable or not course.school.partner.active: # both objects are boolean 
AttributeError: 'nonetype' object has no attribute 'bookable'

解决方法

>有些东西是绑定无最新的.弄清楚它是什么并修复你的逻辑错误. >有些东西是无约束力的.图等

Python 类:TypeError:'NoneType' 对象不可调用

Python 类:TypeError:'NoneType' 对象不可调用

如何解决Python 类:TypeError:''NoneType'' 对象不可调用?

我正在尝试创建一个 Python 版本的 Monopoly。我有一个单独的类,我用它来洗牌和跟踪机会和公益金卡。卡片存储在列表 chest_cardschance_cards 中。

def __init__(self):
    self.chance = random.shuffle(chance_cards)
    self.chest = random.shuffle(chest_cards)
    self.chance_count = 0
    self.chest_count = 0
    
def chance(self):
    self.chance_count += 1
    return self.chance[self.chance_count - 1]

在我的主代码中,我只是在运行

p = cards()
print (p.chance())

测试我的代码,但打印行得到 TypeError: ''nonetype'' object is not callable

有什么想法吗?还是需要看更多的代码? TIA

编辑:这是完整的 cards 课程,如果有帮助的话

import random
global chance_count
global chest_count

class cards:
    global chest_cards
    global chance_cards
    chest_cards = (["Go to Jail","Get Out of Jail Free","Advance to Go (Collect $200)","Bank error in your favor (Collect $200)","Doctor''s fee (Pay $50)","From sale of stock you get $50","Grand Opera Night — Collect $50 from every player","Holiday Fund matures (Collect $100)","Income tax refund (Collect $20)","It is your birthday (Collect $10)","Life insurance matures (Collect $100)","Pay hospital fees of $100","Pay school fees of $150","Receive $25 consultancy fee","You are assessed for street repairs – $40 per house – $115 per hotel","You have won second prize in a beauty contest (Collect $10)","You inherit $100"])

    chance_cards = (["Go to Jail","Advance to Illinois Ave — If you pass Go,collect $200","Advance to St. Charles Place – If you pass Go,"Advance token to nearest Utility. If uNowned,you may buy it from the Bank. If owned,throw dice and pay owner a total ten times the amount thrown.","Advance token to the nearest Railroad and pay owner twice the rental to which he/she is otherwise entitled. If Railroad is uNowned,you may buy it from the Bank.","Bank pays you dividend of $50","Go Back 3 Spaces","Make general repairs on all your property – For each house pay $25  –For each hotel $100","Pay poor tax of $15","Take a trip to Reading Railroad – If you pass Go,"Take a walk on the Boardwalk – Advance token to Boardwalk","You have been elected Chairman of the Board – Pay each player $50","Your building and loan matures — Collect $150","You have won a crossword competition (Collect $100)"])

    def __init__(self):
        self.chance = random.shuffle(chance_cards)
        self.chest = random.shuffle(chest_cards)
        self.chance_count = 0
        self.chest_count = 0
        
    def chance(self):
        self.chance_count += 1
        return self.chance[self.chance_count - 1]

解决方法

当您创建类的实例时(假设它在 class cards: 函数之前是 __init__),您使用名为 chance 的方法创建了一个对象,但是在 {{1}您使用名为 __init__ 的属性覆盖此方法,返回值始终为 chance,因为 shuffle 就地“洗牌”列表,而不是创建具有随机顺序:

random.shuffle

编辑:关于全局变量的注释

摆脱None的选项(你真的应该自己在outside learning上做一些variable scope...):>

  1. 将类外的变量移到“模块范围”中。您仍然可以在课堂上参考它们。
>>> chance_cards = [''card1'',''card2'',''card3'']
>>> chance = random.shuffle(chance_cards)
>>> print(chance)
None
  1. 如果您只在类中需要它们,请将它们保留为类属性,并使用 global 或类名来引用它们。
import random,copy #copy isn''t strictly needed but used for clarity
# `new_list=some_list[:]` is functionally equivalent to `new_list=copy.copy(some_list)`
chest_cards = (["Go to..."])
chance_cards = (["Go to Ja..."])

class cards:
    def __init__(self):
        self.chest_cards = copy.copy(chest_cards) #make a local copy so you don''t alter the master list
        random.shuffle(self.chest_cards)
  1. 肯定有更多的方法...看看你能不能找到任何:)
,

首先:

您有一个名为 chance 的数据属性和一个名为 chance 的方法;因此,当您在 init 方法中设置 self.chance 时,它​​会覆盖对同名方法的引用 - 为您的 chance 属性提供不同的名称`

其次:

random.shuffle() 是一个就地函数——它改变你传递给它的列表,并返回 NONE——这就是为什么你的机会属性被设置为 None。如果您希望您的机会属性成为您的机会全局的一个版本,该版本已打乱 - 则执行以下操作:

def __init__(self):
     self.chance = chance[:]
     random.shuffle(self.chance)

def __init__(self):
     self.chance = list(random.choices(chance,k=len(chance))

第三:

全局变量 - 为什么(养成使用习惯是非常糟糕的) - 如果你打算使用它们(为什么)不要从类体中设置 - 这完全没有必要,而且令人困惑。

Python3基础 type 获得变量的类型

Python3基础 type 获得变量的类型

  •        Python : 3.7.0
  •          OS : Ubuntu 18.04.1 LTS
  •         IDE : PyCharm 2018.2.4
  •       Conda : 4.5.11
  •    typesetting : Markdown

code

coder@Ubuntu:~$ source activate py37
(py37) coder@Ubuntu:~$ ipython
Python 3.7.0 (default, Jun 28 2018, 13:15:42) 
Type ''copyright'', ''credits'' or ''license'' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type ''?'' for help.

In [1]: type(1)
Out[1]: int

In [2]: type(1.1)
Out[2]: float

In [3]: type("cba")
Out[3]: str

In [4]: type(12345678987654321)  # 注:这么大的整数,是否为long?
Out[4]: int

In [5]: exit
(py37) coder@Ubuntu:~$ source deactivate
coder@Ubuntu:~$ 

resource

  • [文档] docs.python.org/3
  • [规范] www.python.org/dev/peps/pep-0008
  • [规范] zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_language_rules
  • [源码] www.python.org/downloads/source
  • [ PEP ] www.python.org/dev/peps
  • [平台] www.cnblogs.com
  • [平台] gitee.com


Python具有开源、跨平台、解释型、交互式等特性,值得学习。
Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。
代码的书写要遵守规范,这样有助于沟通和理解。
每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。

关于确定变量的类型为python中的NoneTypepython的变量,如何确定其类型和能进行的操作?的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Python 3.x中的NoneType在哪里?、python – NoneType和str的不支持的操作数类型、Python 类:TypeError:'NoneType' 对象不可调用、Python3基础 type 获得变量的类型等相关内容,可以在本站寻找。

本文标签: